是否可以使用类似方法\myphantom[xyz]{abcdef}
打印“xyz”并进行一些对齐,但留下空格,就像“abcdef”一样?
类似这样的代码:
\[mn xyz \myphantom[printed]{abcdefghi abcdefghi abcdefghi} 123 456\]
\[mn xyz abcdefghi abcdefghi abcdefghi 123 456\]
要打印这样的内容:
mn printed 123 456
mn abcdefghi abcdefghi abcdefghi 123 456
或者:
mn printed 123 456
mn abcdefghi abcdefghi abcdefghi 123 456
或者:
mn printed 123 456
mn abcdefghi abcdefghi abcdefghi 123 456
谢谢
答案1
让我们做出三个定义:
\documentclass{article}
\begin{document}
\newlength{\phantomwidth}
\def\myphantom#1#2{\settowidth{\phantomwidth}{{#2}}\makebox[\phantomwidth]{{#1}}}
\def\myphantoml#1#2{\settowidth{\phantomwidth}{{#2}}\makebox[\phantomwidth][l]{{#1}}}
\def\myphantomr#1#2{\settowidth{\phantomwidth}{{#2}}\makebox[\phantomwidth][r]{{#1}}}
mn xyz \myphantom{printed}{abcdefghi abcdefghi abcdefghi} 123 456
mn xyz abcdefghi abcdefghi abcdefghi 123 456
mn xyz \myphantoml{printed}{abcdefghi abcdefghi abcdefghi} 123 456
mn xyz abcdefghi abcdefghi abcdefghi 123 456
mn xyz \myphantomr{printed}{abcdefghi abcdefghi abcdefghi} 123 456
mn xyz abcdefghi abcdefghi abcdefghi 123 456
\end{document}
然而,使用tabular
可能是更自然的选择。
答案2
我介绍了该命令的文本模式\myphantom
和数学模式\mathphantom
版本。数学模式会自动检测当前的数学样式并相应地调整大小。这两个版本都带有可选的放置参数([l]、[c] 或 [r])。
\documentclass{article}
\usepackage{scalerel}
\global\newlength\thewidth
\newcommand\findwidth[1]{\setbox0\hbox{#1}\setlength\thewidth{\wd0}}
\newcommand\myphantom[3][c]{\findwidth{#3}\makebox[\thewidth][#1]{#2}}
\newcommand\mathphantom[3][c]{\ThisStyle{%
\findwidth{$\SavedStyle#3$}\makebox[\thewidth][#1]{$\SavedStyle#2$}
}}
\parindent 0in
\begin{document}
Text Mode:
mn xyz \myphantom{printed}{abcdefghi abcdefghi abcdefghi} 123 456
mn xyz \myphantom[l]{printed}{abcdefghi abcdefghi abcdefghi} 123 456
mn xyz \myphantom[r]{printed}{abcdefghi abcdefghi abcdefghi} 123 456
mn xyz abcdefghi abcdefghi abcdefghi 123 456
Math Mode (it auto-determines math display style)
\[
y^{3/2} = \mathphantom{x^3}{\sum_i(A x^2 + B x + C)} + D/x
\]
\[
y^{3/2} = {\sum_i(A x^2 + B x + C)} + D/x
\]
\(
y^{3/2} = \mathphantom{x^3}{\sum_i(A x^2 + B x + C)} + D/x
\)
\(
y^{3/2} = {\sum_i(A x^2 + B x + C)} + D/x
\)
\end{document}
需要psscalerel
版本 1.5。