如何将段落的宽度与线宽的 45% 进行比较?

如何将段落的宽度与线宽的 45% 进行比较?

这是我的伪乳胶代码,试图展示我在备忘录乳胶课中要做的事情:

\newcommand\headername{%should be the @memoto, or if too long, an abbreviated subject
    \ifthenelse{\lengthtest{\settowidth{\@memoto}>.45*\linewidth}}
        {\emph{dummy text here}}
        {\@memoto}
}

我可能缺少一些计算包或其他东西,但我正在寻找某种测试来查看 \@memoto 是否长于线宽的 45%。我该怎么做?

(最终我将在 LyX 中使用这个乳胶类,但我希望这足够通用,可以在不考虑 LyX 的情况下回答)

答案1

这是一个选项:

在此处输入图片描述

\documentclass{article}
\makeatletter
\newcommand{\memoto}{\@dblarg\memoto@aux}
\def\memoto@aux[#1]#2{%
  \def\@memoto@short{#1}% Short \@memoto
  \def\@memoto@long{#2}% Long \@memoto
}
\newcommand\headername{% should be \@memoto, or if too long, an abbreviated subject
  \setbox9=\hbox{\@memoto@long}%
  \ifdim\wd9>.45\linewidth
    \emph{\@memoto@short}%
  \else
    \@memoto@long%
  \fi
}
\makeatother
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
\rule{.45\textwidth}{1pt}\par
\memoto[Here is some short text]{Here is some longer text}\headername\par
\memoto[Here is some short text]{Here is some much longer text}\headername\par
\memoto[Here is some short text]{Here is some very much longer text}\headername\par
\memoto[Here is some short text]{Here is some extremely much longer text}\headername\par
\rule[1ex]{.45\textwidth}{1pt}\par
\end{document}

\memoto使用的定义\@dblarg允许轻松指定可选参数(参见\@dblarg课堂上的命令起什么作用?),如果较长文本的长度大于.45\textwidth,则使用较短的文本。

相关内容