![文本以指定字符为中心](https://linux22.com/image/445325/%E6%96%87%E6%9C%AC%E4%BB%A5%E6%8C%87%E5%AE%9A%E5%AD%97%E7%AC%A6%E4%B8%BA%E4%B8%AD%E5%BF%83.png)
这听起来像是一个简单的问题,但是我怎样才能使文本右对齐和左对齐,即以预定义的中间字符为中心(最好在文本模式下并且没有表格)?
本质上这就是我所寻找的:
Foo – Bar
Foofoobar – Splat
Some other long entry – And we're done.
我尝试过tabto
,\hfill
以及其他一些方法,但似乎没有什么效果。有什么建议吗?
答案1
答案2
为了测量左侧的宽度,您可以将左侧的所有行放入\hbox
嵌套在 a 中的 es 中,\vbox
然后依次放入 a 中。然后对左侧的每一行\hphantom
执行 an :\llap
\documentclass{article}
\newcommand\phantombox[2]{%
\leavevmode\hphantom{\vbox{#1}}\llap{#2}%
}%
\begin{document}
\noindent text \hfill text \hfill text \hfill text
\hbox{\phantombox{\hbox{Foo}\hbox{Foofoobar}\hbox{Some other long enty}}{Foo} -- \hbox{Bar}}
\hbox{\phantombox{\hbox{Foo}\hbox{Foofppbar}\hbox{Some other long enty}}{Foofoobar} -- \hbox{Splat}}
\hbox{\phantombox{\hbox{Foo}\hbox{Foofppbar}\hbox{Some other long enty}}{Some other long enty} -- \hbox{And we're done.}}%
\par
\end{document}
这可以自动化:
\makeatletter
\newcommand\leftright[2]{%
\ifvmode\else\par\fi
\leftrightloop{#1}#1{}{}\relax#2{}{}\relax
}%
\@ifdefinable\leftrightloop{%
\long\def\leftrightloop#1#2#3\relax#4#5\relax{%
\ifcat$\detokenize{#3#5}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\par}{%
\ifcat$\detokenize{#3}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\hbox{\hphantom{\vbox{\hboxloop#1{}{}\relax}} -- \hbox{#4}}%
\leftrightloop{#1}{}{}\relax#5\relax
}{%
\ifcat$\detokenize{#5}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\hbox{\hphantom{\vbox{\hboxloop#1{}{}\relax}}\llap{#2}}%
\leftrightloop{#1}#3\relax{}{}\relax
}{%
\hbox{\hphantom{\vbox{\hboxloop#1{}{}\relax}}\llap{#2} -- \hbox{#4}}%
\leftrightloop{#1}#3\relax#5\relax
}%
}%
}%
}%
}%
\@ifdefinable\hboxloop{%
\long\def\hboxloop#1#2\relax{%
\ifcat$\detokenize{#2}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{}{\hbox{#1}\hboxloop#2\relax}%
}%
}%
\makeatother
\documentclass{article}
\begin{document}
\noindent text \hfill text \hfill text \hfill text
\leftright{{Foo}{Foofoobar}{Some other long enty}}%
{{Bar}{Splat}{And we're done.}}%
\end{document}
如果您需要将其置于页面文本宽度/周围垂直框宽度的中心:
\makeatletter
\newcommand\leftright[2]{%
\ifvmode\else\par\fi
\leftrightloop{#1}{#2}#1{}{}\relax#2{}{}\relax
}%
\@ifdefinable\leftrightloop{%
\long\def\leftrightloop#1#2#3#4\relax#5#6\relax{%
\ifcat$\detokenize{#4#6}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\par}{%
\ifcat$\detokenize{#4}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\hbox to\hsize{%
\hss
\hphantom{\vbox{\hboxloop#1{}{}\relax}} --
\rlap{#5}%
\hphantom{\vbox{\hboxloop#2{}{}\relax}}%
\hss
}%
\leftrightloop{#1}{#2}{}{}\relax#6\relax
}{%
\ifcat$\detokenize{#6}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\hbox to\hsize{%
\hss
\hphantom{\vbox{\hboxloop#1{}{}\relax}}\llap{#3}
\hphantom{--}
\hphantom{\vbox{\hboxloop#2{}{}\relax}}%
\hss
}%
\leftrightloop{#1}{#2}#4\relax{}{}\relax
}{%
\hbox to\hsize{%
\hss
\hphantom{\vbox{\hboxloop#1{}{}\relax}}\llap{#3} --
\rlap{#5}%
\hphantom{\vbox{\hboxloop#2{}{}\relax}}%
\hss
}%
\leftrightloop{#1}{#2}#4\relax#6\relax
}%
}%
}%
}%
}%
\@ifdefinable\hboxloop{%
\long\def\hboxloop#1#2\relax{%
\ifcat$\detokenize{#2}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{}{\hbox{#1}\hboxloop#2\relax}%
}%
}%
\makeatother
\documentclass{article}
\begin{document}
\noindent text \hfill text \hfill text \hfill text
\leftright{{Foo}{Foofoobar}{Some other long enty}}%
{{Bar}{Splat}{And we're done.}}%
\end{document}
如果您希望分隔短划线位于页面文本宽度/周围垂直框宽度的中心:
\makeatletter
\newcommand\leftright[2]{%
\ifvmode\else\par\fi
\leftrightloop{#1#2}#1{}{}\relax#2{}{}\relax
}%
\@ifdefinable\leftrightloop{%
\long\def\leftrightloop#1#2#3\relax#4#5\relax{%
\ifcat$\detokenize{#3#5}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{\par}{%
\ifcat$\detokenize{#3}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\hbox to\hsize{%
\hss
\hphantom{\vbox{\hboxloop#1{}{}\relax}} --
\rlap{#4}%
\hphantom{\vbox{\hboxloop#1{}{}\relax}}%
\hss
}%
\leftrightloop{#1}{}{}\relax#5\relax
}{%
\ifcat$\detokenize{#5}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{%
\hbox to\hsize{%
\hss
\hphantom{\vbox{\hboxloop#1{}{}\relax}}\llap{#2}%
\hphantom{--}
\hphantom{\vbox{\hboxloop#1{}{}\relax}}%
\hss
}%
\leftrightloop{#1}#3\relax{}{}\relax
}{%
\hbox to\hsize{%
\hss
\hphantom{\vbox{\hboxloop#1{}{}\relax}}\llap{#2} --
\rlap{#4}%
\hphantom{\vbox{\hboxloop#1{}{}\relax}}%
\hss
}%
\leftrightloop{#1}#3\relax#5\relax
}%
}%
}%
}%
}%
\@ifdefinable\hboxloop{%
\long\def\hboxloop#1#2\relax{%
\ifcat$\detokenize{#2}$\expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
{}{\hbox{#1}\hboxloop#2\relax}%
}%
}%
\makeatother
\documentclass{article}
\begin{document}
\noindent text \hfill text \hfill text \hfill text
\leftright{{Foo}{Foofoobar}{Some other long enty}}%
{{Bar}{Splat}{And we're done.}}%
\end{document}
答案3
(更新了答案,提供了第三种解决方案来跟进 David C 的评论)
center
这里还有另外三种可能的解决方案。第一种使用和环境的组合tabular
,第二种使用单个longtable
环境;这两种解决方案都将整个表格状材料水平居中。第三种解决方案也采用了环境tabular
,但它将材料集中在短划线上。
如果前两种解决方案适合一页且不需要分页符,则它们会生成相同的输出。如果您的观点和反驳列表相当长,并且允许在列表内的某个地方进行分页,则第二种解决方案可能更可取。
这些\hrule
指令用于指示文本块的宽度。
\documentclass{article}
\usepackage{longtable} % for 'longtable' environment
\usepackage{array} % for 'w' column type
\usepackage{calc} % for '\widthof' macro
\newlength\mylen % calculate column widths for third solution
\setlength\mylen{(\textwidth-\widthof{\space\textendash\space})/2}
\begin{document}
\hrule
\begin{center}
\begin{tabular}{ r @{\space\textendash\space} l }
Foo & Bar \\
Foofoobar & Splat \\
Some other long entry & And we're done.
\end{tabular}
\end{center}
\hrule
\begin{longtable}{ r @{\space\textendash\space} l }
Foo & Bar \\
Foofoobar & Splat \\
Some other long entry & And we're done.
\end{longtable}
\hrule
\begin{center}
\begin{tabular}{ @{} w{r}{\mylen} @{\space\textendash\space} w{l}{\mylen} @{} }
Foo & Bar \\
Foofoobar & Splat \\
Some other long entry & And we're done.
\end{tabular}
\end{center}
\hrule
\end{document}