即时调整字体大小

即时调整字体大小

在我的文档中,我想通过将一些关键点放在圆角框中来强调它们。我使用以下代码来实现这一点:

\documentclass{report}
\usepackage{fancybox}

\begin{document}

\ovalbox{
\begin{minipage}[c][.35\textwidth]{.5\textwidth} % [text in box][height][width]
\begin{center}
\begin{minipage}[c]{.9\textwidth}
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
\end{minipage}
\end{center}
\end{minipage}
}

\end{document}

我希望修复框的尺寸。当文本现在变得太大而无法容纳框时,我可以让 LateX 注意到这一点,并让它调整字体大小是否合适?

我已获悉此事通过调整字体大小使文本适合给定的框问题,但无法将答案转化为对我来说可行的解决方案(即解决\ovalbox它)。

此外,我是否注意到提出的解决方案中的段落不再缩进,如何避免这种情况?

答案1

\documentclass{report}
\usepackage{lmodern}
\usepackage{fancybox,environ}

\NewEnviron{textinoval}
  {\def\trysize{10.5pt}\def\tryblskip{12.5pt}%
   \retry}
\makeatletter
\def\retry{%
  \ifdim\trysize<5.5pt
    \WARNING\expandafter\@gobble
  \else
    \expandafter\@firstofone
  \fi
  {\edef\trysize{\the\dimexpr\trysize-0.5pt\relax}%
   \edef\tryblskip{\the\dimexpr\tryblskip-0.5pt\relax}%
   \sbox0{\begin{minipage}{.45\textwidth}
   \begingroup\edef\x{\endgroup
     \noexpand\fontsize{\trysize}{\tryblskip}\noexpand\selectfont}\x
   \BODY\end{minipage}}%
  }%
  \dimen0=\dimexpr\ht0+\dp0\relax
  \ifdim\dimen0>.3\textwidth
    \expandafter\retry
  \else
    \expandafter\outputoval
  \fi}
\makeatletter

\def\WARNING{\sbox{0}{WARNING}}

\def\outputoval{\ovalbox{\begin{minipage}[c][.35\textwidth]{.5\textwidth}
  \begin{center}\usebox{0}\end{center}\end{minipage}}}
\begin{document}

\begin{textinoval}
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
\end{textinoval}

\begin{textinoval}
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
\end{textinoval}

\begin{textinoval}
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
\end{textinoval}

\begin{textinoval}
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
\end{textinoval}

\end{document}

在此处输入图片描述

这里有一些解释。首先,\NewEnviron-defined 环境收集其内容。此内容在“私有”框中排版,如果符合大小要求,则将其传递给最终排版;否则,我们尝试以小半个点的大小重新排版,直到满足要求。可以\WARNING根据需要重新定义;当递归启动并且字体已经是 5pt 大小时,它将生效。

答案2

对于一般情况,请参阅通过调整字体大小使文本适合给定的框。对于您的具体情况,您可以使用以下方法,根据需要缩放内容。我并不是说这在所有情况下都会产生很好的结果。

请注意,使用minipage会修改 ,\textwidth因此不同位置的长度可能不同。还请记住,后面的adjustbox键可能会影响前面的键,因为:

\documentclass{report}
\usepackage{fancybox}
\usepackage{adjustbox}

\newenvironment{myovalbox}{%
    \noindent
    \adjustbox{minipage=[c]{.45\textwidth},margin=1ex,max totalsize={\textwidth}{.7\textwidth},center=.5\textwidth,
    env={minipage}[c][.35\textwidth]{.5\textwidth},
    precode=\ovalbox}\bgroup
}{%
    \egroup
}

\begin{document}

\begin{myovalbox}
This text is small enough to fit in the box.
\end{myovalbox}

\begin{myovalbox}
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
\end{myovalbox}

\begin{myovalbox}
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
\end{myovalbox}

\begin{myovalbox}
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
\end{myovalbox}

\begin{myovalbox}
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
This text might be too large to fit into the (.35x.50)$\cdot$textwidth box,
in which case the font size should be decreased to make it fit.
\end{myovalbox}

\end{document}

结果

相关内容