文本以指定字符为中心

文本以指定字符为中心

这听起来像是一个简单的问题,但是我怎样才能使文本右对齐和左对齐,即以预定义的中间字符为中心(最好在文本模式下并且没有表格)?

本质上这就是我所寻找的:

                  Foo – Bar
            Foofoobar – Splat
Some other long entry – And we're done.

我尝试过tabto\hfill以及其他一些方法,但似乎没有什么效果。有什么建议吗?

答案1

在此处输入图片描述

\documentclass{article}

\begin{document}
\begin{center}
\makebox[0pt][r]{Foo} – \makebox[0pt][l]{Bar}

\makebox[0pt][r]{Foofoobar} – \makebox[0pt][l]{Splat}

\makebox[0pt][r]{Some other long enty} – \makebox[0pt][l]{And we're done.}
\end{center}
\end{document}

零宽度框隐藏了不同长度的文本。

答案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}

相关内容