使用 adjustbox 包将标题字体调整为缩小的数字

使用 adjustbox 包将标题字体调整为缩小的数字

我有一页包含多个水平对齐的图形。可能会发生许多图形需要放在此页面的情况。因此,我使用 adjustbox 包将它们缩小以适合页面。但是,这意味着标题也会缩小,并且很多时候变得不可读。因此,我尝试通过 adjustbox 包自动调整标题的字体大小以适应缩放比例。当 adjustbox 缩小内容(图形及其标题)时,我的新环境应该会自动增加标题的字体。

我当前的代码似乎已经接近这一点。使用“precode”键,我检索了原始内容的宽度,名为“wdtotal”。紧接着,我尝试使用“innercode”命令来设置字幕的字体大小。但是,“innercode”键无法识别“wdtotal”变量。换句话说,在检索原始宽度后,我无法将其导出到adjustbox的其他键中,以便相应地设置字幕字体。

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

\newlength{\wdtotal}

\newenvironment{figfit}{%
    \adjustboxset{precode={\global\setlength\wdtotal{\width}}%
    \adjustboxset{innercode={\ifdim\wdtotal>\linewidth%
                                  \captionsetup{font=huge}%
                             \fi}{\captionsetup{font=normalsize}}
    \begin{adjustbox}{max totalsize={\linewidth}{\availableheight},%
                      keepaspectratio,%
                      center=\linewidth}}%
    {\hfill\end{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}```

答案1

这会将所有图像缩放到相同的量,但根本不会缩放标题。我认为这\availableheight应该等于页面上的剩余空间。

\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}{\par\vskip\intextsep
  \sbox0{\renewcommand\captionof[2]{\par}% measure size of images
    \begin{varwidth}[b]{2\paperwidth} \BODY \end{varwidth}}% note 2\paperwidth max width
  \count1=\value{figure}%
  \setbox1=\vbox{\renewcommand{\includegraphics}[2][]{\hrule height0pt }\BODY}% measure height of captions
  \setcounter{figure}{\count1}% restore global counter
  \def\myscale{1}% compute scale
  \ifdim\wd0>\linewidth
    \pgfmathsetmacro{\myscale}{\linewidth / \wd0}%
  \fi
  \setlength{\availableheight}{\dimexpr \pagegoal-\pagetotal-\ht1-\dp1-\baselineskip\relax}% remaining space - captions
  \ifdim\myscale\ht0>\availableheight
    \pgfmathsetmacro{\myscale}{\availableheight / \ht0}%
  \fi
  \let\normalincludegraphics=\includegraphics
  \renewcommand{\includegraphics}[2][]{\normalincludegraphics[scale=\myscale,##1]{##2}}%
  \centering
  \BODY
  \par\vskip\intextsep}

\begin{document}
Fit images to width.

\fboxsep=0pt
\fbox{\begin{minipage}{0.4\textwidth}% 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}

相关内容