Guest User

Untitled

a guest
Nov 6th, 2018
90
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. \documentclass{article}
  2.  
  3. \usepackage{graphicx,subcaption,xfp,etoolbox,letltxmacro}
  4.  
  5. \makeatletter
  6. \newcommand{\setscalefactor}[2]{%
  7.   \def\scalefactor{9999}% Default (large) scale factor
  8.   \renewcommand*{\do}[1]{% How each element will be processed
  9.     \settowidth{\@tempdima}{##1}% Store width of element
  10.     \ifdim\scalefactor pt > \fpeval{(#1) / \@tempdima} pt
  11.      \xdef\scalefactor{\fpeval{(#1) / \@tempdima}}% Update scaling factor
  12.     \fi
  13.   }%
  14.   \docsvlist{#2}% Process list of elements
  15.   % Change the default scale factor for \includegraphics
  16.   \LetLtxMacro\oldincludegraphics\includegraphics
  17.   \renewcommand{\includegraphics}[2][]{%
  18.     \oldincludegraphics[scale = \scalefactor, ##1]{##2}%
  19.   }
  20. }
  21.  
  22. \newcommand{\setandscaleimages}[2]{%
  23.   \setscalefactor{#1}{#2}% Set \scalefactor
  24.   % Set images as subfloats
  25.   % https://tex.stackexchange.com/a/89187/5764
  26.   \def\subfloatdelim{\def\subfloatdelim{\hfill}}% Subfloat delimiter delayed by one cycle
  27.   \renewcommand*{\do}[1]{% How each item is processed
  28.     \subfloatdelim
  29.     \subcaptionbox{}[#1]{##1}%
  30.   }%
  31.   \docsvlist{#2}% Process CSV list
  32. }
  33.  
  34. \makeatother
  35.  
  36. \begin{document}
  37.  
  38. \begin{figure}
  39.  
  40.   % Identify the scale factor to fit content within 0.4\linewidth
  41.   \setandscaleimages{0.4\linewidth}{%
  42.     \includegraphics{example-image}, % First image
  43.     \includegraphics{example-image-a4-landscape} % Second image
  44.   }
  45.   \caption{A figure caption}
  46. \end{figure}
  47.  
  48. \end{document}
RAW Paste Data