答案1
您可以使用以下命令检查参数的宽度是否大于给定的宽度\ifdim
:
\documentclass{article}
\begin{document}
\makeatletter
\newcommand\newparbox[3][]{%
\settowidth\@tempdima{#3}
\ifdim\@tempdima>#2\relax
\parbox[#1]{#2}{#3}%
\else
\makebox[#2][s]{#3}%
\fi
}
\makeatother
|\newparbox{8em}{AB AB}|\par
|\newparbox[t]{8em}{AB AB AB AB AB AB AB}|
\end{document}
编辑:LaTeX 版本:
\documentclass{article}
\usepackage{ifthen}
\begin{document}
\makeatletter
\newcommand\newparbox[3][]{%
\settowidth\@tempdima{#3}%
\ifthenelse{\lengthtest{\@tempdima > #2}}{\parbox[#1]{#2}{#3}}{\makebox[#2][s]{#3}}%
}
\makeatother
|\newparbox{8em}{AB AB}|\par
|\newparbox[t]{8em}{AB AB AB AB AB AB AB}|
\end{document}