我最近发现自己在写文档时,文本是“居中并排的”。我解决这个问题的方法是使用环境align
。例如,代码
\documentclass{beamer}
\usepackage{amsmath}
\begin{document}
Two great bands are
\begin{align*}
\textnormal{Led Zeppelin} && \textnormal{Pink Floyd}
\end{align*}
The members of the Beatles were
\begin{align*}
\textnormal{John Lennon} && \textnormal{Paul McCartney} && \textnormal{Ringo Starr} && \textnormal{George Harrison}
\end{align*}
\end{document}
生产
有没有更简单的方法可以做到这一点?
我目前正在与beamer
班级合作,但最好有一个通用的解决方案。
答案1
你可以利用在两侧center
添加胶水的事实:\hfil
\documentclass[a4paper]{article}
\usepackage{xparse}
\usepackage{showframe} % for debugging
\ExplSyntaxOn
\NewDocumentCommand{\gcenter}{m}
{
\begin{center}
\seq_set_split:Nnn \l_tmpa_seq { & } { #1 }
\seq_use:Nn \l_tmpa_seq { \hfil }
\end{center}
}
\ExplSyntaxOff
\begin{document}
Two great bands are
\gcenter{Led Zeppelin & Pink Floyd}
The members of the Beatles were
\gcenter{John Lennon & Paul McCartney & Ringo Starr & George Harrison}
More text here
\end{document}
如果需要多条线路,则还需要做更多的工作。
\documentclass[a4paper]{article}
\usepackage{xparse}
\usepackage{showframe}
\ExplSyntaxOn
\NewDocumentCommand{\gcenter}{m}
{
\begin{center}
\seq_set_split:Nnn \l_tmpa_seq { \\ } { #1 }
\seq_map_inline:Nn \l_tmpa_seq
{
\seq_set_split:Nnn \l_tmpb_seq { & } { ##1 }
\seq_use:Nn \l_tmpb_seq { \hfil }
\\
}
\end{center}
}
\ExplSyntaxOff
\begin{document}
Two great bands are
\gcenter{Led Zeppelin & Pink Floyd}
The members of the Beatles were
\gcenter{John Lennon & Paul McCartney & Ringo Starr & George Harrison}
Here are two lines
\gcenter{
Led Zeppelin & Pink Floyd \\
John Lennon & Paul McCartney & Ringo Starr & George Harrison
}
More text here
\end{document}