TransWikia.com

Adjust caption font to figures scaled down by the adjustbox package

TeX - LaTeX Asked by Panos Evangeliou on March 11, 2021

I have a page with multiple figures, horizontally aligned. It can happen that many figures need to be fitted in this page. Thus, I am using the adjustbox package to scale them down to fit into the page. However, this means that the captions are also scaled down and many times become unreadable.
Therefore, I am trying to automatically adjust the font size of my captions to the scaling by the adjustbox package. When the adjustbox scales down the content (figures and their captions), my new environment should automatically increase the font of the captions.

My current code seems to be reaching close to that point. Using the "precode" key I retrieve the width of the original content, named "wdtotal". Right after, I try to use the "innercode" command to setup the captions’ font size. However, the "wdtotal" variable is not recognized by the "innercode" key. In other words, after retrieving the original width, I cannot export it into other keys of the adjustbox in order to setup the caption font accordingly.

documentclass{scrartcl} 
usepackage{adjustbox}
usepackage{caption}

newlength{wdtotal}

newenvironment{figfit}{%
    adjustboxset{precode={globalsetlengthwdtotal{width}}%
    adjustboxset{innercode={ifdimwdtotal>linewidth%
                                  captionsetup{font=huge}%
                             fi}{captionsetup{font=normalsize}}
    begin{adjustbox}{max totalsize={linewidth}{availableheight},%
                      keepaspectratio,%
                      center=linewidth}}%
    {hfillend{adjustbox}}%


begin{document}
Some text here.
begin{figfit}
    includegraphics{1.png}captionof{figure}{Increase my font, if figure scales down}vfill%
    includegraphics{2.png}captionof{figure}{Increase my font, if figure scales down}vfill%
    includegraphics{3.png}captionof{figure}{Increase my font, if figure scales down}vfill%
end{figfit}

end{document}```

One Answer

This scales all the images the same amount but does not scale the captions at all. I assume that availableheight is supposed to equal the remaining space on the page.

documentclass{scrartcl} 
usepackage{graphicx}
usepackage{caption}
usepackage{environ}% for BODY
usepackage{pgfmath}% or tikz
usepackage{varwidth}

usepackage{lipsum}% MWE only
usepackage{showframe}% MWE only

newlength{availableheight}

NewEnviron{figfit}{parvskipintextsep
  sbox0{renewcommandcaptionof[2]{par}% measure size of images
    begin{varwidth}[b]{2paperwidth} BODY end{varwidth}}% note 2paperwidth max width
  count1=value{figure}%
  setbox1=vbox{renewcommand{includegraphics}[2][]{hrule height0pt }BODY}% measure height of captions
  setcounter{figure}{count1}% restore global counter
  defmyscale{1}% compute scale
  ifdimwd0>linewidth
    pgfmathsetmacro{myscale}{linewidth / wd0}%
  fi
  setlength{availableheight}{dimexpr pagegoal-pagetotal-ht1-dp1-baselineskiprelax}% remaining space - captions
  ifdimmyscaleht0>availableheight
    pgfmathsetmacro{myscale}{availableheight / ht0}%
  fi
  letnormalincludegraphics=includegraphics
  renewcommand{includegraphics}[2][]{normalincludegraphics[scale=myscale,##1]{##2}}%
  centering
  BODY
  parvskipintextsep}

begin{document}
Fit images to width.

fboxsep=0pt
fbox{begin{minipage}{0.4textwidth}% adjust linewidth
begin{figfit}%
    includegraphics{example-image-a}captionof{figure}{Increase my font, if figure scales down}vfill%
    includegraphics{example-image-b}captionof{figure}{Increase my font, if figure scales down}vfill%
    includegraphics{example-image-c}captionof{figure}{Increase my font, if figure scales down}vfill%
end{figfit}%
end{minipage}}

newpage
Fit images to height.

lipsum[1]

begin{figfit}%
    includegraphics{example-image-a}captionof{figure}{Increase my font, if figure scales down}vfill%
    includegraphics{example-image-b}captionof{figure}{Increase my font, if figure scales down}vfill%
    includegraphics{example-image-c}captionof{figure}{Increase my font, if figure scales down}vfill%
end{figfit}%

end{document}

Answered by John Kormylo on March 11, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP