我想创建一个 (par)box,它从我现在所在的位置开始,一直延伸到线的末尾。我注意到这个相关问题:
事实上,如果我能得到位置,我就能做位置算术;但也许还有其他更简单的方法可以做到?
答案1
以下用途zref
模块savepos
用于存储调用时的 x 坐标\fillparbox
,以及发出后的 x 坐标。这两个 x 坐标之间的差异提供了可以放置在那里的\hfill
的宽度:\parbox
\documentclass{article}
\usepackage[margin=1in]{geometry}
\usepackage[nopar]{lipsum}
\usepackage{zref-savepos}
\newcounter{fillparbox}
\newcommand{\fillparbox}[2][c]{%
\stepcounter{fillparbox}% New \fillparbox
\mbox{}\zsaveposx{\thefillparbox-L}\hfill\zsaveposx{\thefillparbox-R}% Mark Left and Right end-points remaining on line.
\makebox[0pt][r]{\parbox[#1]{\dimexpr\zposx{\thefillparbox-R}sp-\zposx{\thefillparbox-L}sp}{\strut #2\strut}}%
}
\begin{document}
\lipsum[1]
\fillparbox{\itshape\bfseries one two three four five six seven eight nine ten
eleven twelve thirteen}
\lipsum[2]
\fillparbox{\itshape\bfseries one two three four five six seven eight nine ten
eleven twelve thirteen}
\lipsum[3]
\fillparbox[t]{\itshape\bfseries one two three four five six seven eight nine ten
eleven twelve thirteen}
\end{document}
您仍然可以发出与 关联的各种对齐选项\parbox
,默认为[c]
输入(垂直)。\strut
插入 以确保基线一致(参见\baselineskip
使用minipage
s (或es) 时如何保持常数\parbox
?)。
答案2
您可以使用 pdftex\pdfsavepos
扩展来记录线上的点。这在 xetex 中也有效,luatex85
在 luatex 中需要进行一些名称更改(或使用包)。
\documentclass{article}
\makeatletter
\newcount\zzcount
\def\zz#1{\global\advance\zzcount\@ne
\edef\tmp{\pdfsavepos\write\@auxout{\gdef\string\ZZ\romannumeral\zzcount{\noexpand\the\pdflastxpos}}}%
\tmp
\expandafter\ifx\csname ZZ\romannumeral\zzcount\endcsname\relax
\message{first pass re-run latex to see zz box}%
\dotfill
\else
\dimen@\dimexpr\csname ZZZ\romannumeral\zzcount\endcsname sp-\csname \romannumeral\zzcount\endcsname sp\relax
\ifdim\dimen@>\p@
\parbox[t]%
{\dimen@}%
{\itshape\bfseries#1}\hfill
\else
\message{something wrong try deleting aux file}%
XX\dotfill
\fi
\fi
\edef\tmp{\pdfsavepos\write\@auxout{\gdef\string\ZZZ\romannumeral\zzcount{\noexpand\the\pdflastxpos}}}
\tmp
}
\begin{document}
aaaa aaa aaa aaaaaaa aaa aaa aaaaaaa aaa aaa aaaaaaa aaa aaa aaa
aaaa aaa aaa aaaaaaa aaa aaa aaaaaaa aaa aaa aaaaaaa aaa aaa aaa
aaaa aaa aaa aaaaaaa aaa aaa aaaaaaa aaa aaa aaaaaaa aaa aaa aaa
aaa aaa aaaa aaa aaa aaa
\zz{one two three four five six seven eight nine ten
eleven twelve thirteen}
aaa aaa aaaa aaa aaa aaa
bbb bb bbb bb bbb bbbb
\zz{one two three four five six seven eight nine ten
eleven twelve thirteen}
\end{document}