宏可排列多组列以跨越整个宽度

宏可排列多组列以跨越整个宽度

我正在通过外部脚本向 LaTeX 模板提供内容,而阻碍我的最后一个格式化步骤是:\equispace[6]{a,b,c}{d,e,f}{g,h}{i,j,k,l}{m,n,o}{p,q,r,s,t}将参数转换为这种数组的宏,

在此处输入图片描述

当数组延伸到整个线宽时,项目组间距相等,并围绕其自己的组居中。我尝试使用 tabularx 和 minipage,但这比我有限的 TeX 技能允许的要棘手。请注意,我事先知道每个组中有多少个项目,因此如果有很好的方法来解析内容,我可以使用硬编码模板。

答案1

忽略[6]论点(参见问题下的评论)然后

在此处输入图片描述

\documentclass{article}

\newcommand\equispace[6]{%
\begin{center}%
\begin{tabular}{@{}c@{}}\zz{#1}\\\zz{#4}\end{tabular}\hfill
\begin{tabular}{@{}c@{}}\zz{#2}\\\zz{#5}\end{tabular}\hfill
\begin{tabular}{@{}c@{}}\zz{#3}\\\zz{#6}\end{tabular}%
\end{center}%
}

\makeatletter
\def\zz#1{\@for\z:=#1\do{\z\,}}
\makeatother

\begin{document}

\equispace{a,b,c}{d,e,f}{g,h}{i,j,k,l}{m,n,o}{p,q,r,s,t}

\end{document}

答案2

如果你运行外部脚本,它可能会输出如下内容:

\begin{tabular*}{\textwidth}{@{}c@{\extracolsep{\fill}}c@{\extracolsep{\fill}}c@{}}
  a b c &   g h   & m n o\\
  d e f & i j k l & p q r s t
\end{tabular*}

输出: 在此处输入图片描述

带有线条:

\begin{tabular*}{\textwidth}{|@{}c@{\extracolsep{\fill}}c@{\extracolsep{\fill}}c@{}|}
  \hline
  a b c &   g h   & m n o\\
  \hline
  d e f & i j k l & p q r s t\\
  \hline
\end{tabular*}

在此处输入图片描述

编辑:

下面的代码应该能够解析任意数量的参数(并像您的示例中那样对其进行排序)。代码可能并不完美,也许可以改进,但它确实可以处理迄今为止我遇到的所有问题。

\documentclass[border=2mm]{standalone}

\makeatletter
\newcount\equi@columns
\newcount\equi@rows
\newcount\equi@args
\newcount\equi@currow
\newcount\equi@curcol
\newcommand\equispace*{}
\def\equispace%
  {%
    \equi@currow=\@ne
    \equi@curcol=\@ne
    \@ifnextchar<
      {\equispace@i}
      {\equispace@i<3>}%
  }
\def\equispace@i<#1>%
  {%
    \equi@columns=#1
    \@ifnextchar[
      {\equispace@ii}
      {\equispace@ii[6]}%
  }
\def\equispace@ii[#1]%
  {%
    \equi@args=#1
    \equi@rows=\numexpr\equi@args/\equi@columns\relax
    \ifnum\numexpr\equi@rows*\equi@columns<\equi@args
      \advance\equi@rows by \@ne
    \fi
    \equi@readarg
  }
\newcommand\equi@readarg[1]
  {%
    \advance\equi@args by \m@ne
    \expandafter\equi@addto@row\expandafter
      {\csname equi@row@\the\equi@currow\endcsname}{\equi@format{#1}}%
    \advance\equi@currow by \@ne
    \ifnum\equi@currow>\equi@rows
      \equi@currow=\@ne
      \advance\equi@curcol by \@ne
    \fi
    \ifnum\equi@args=0
      \expandafter\equi@output
    \else
      \expandafter\equi@readarg
    \fi
  }
\newcommand\equi@addto@row[2]
  {%
    \ifnum\equi@curcol=\@ne
      \def#1{#2}%
    \else
      \edef#1{\unexpanded\expandafter{#1&#2}}%
    \fi
  }
\newcommand\equi@addto@colspec[1]
  {\edef\equi@colspec{\unexpanded\expandafter{\equi@colspec#1}}}
\newcommand*\equi@output
  {%
    \def\equi@colspec{@{}c}%
    \equi@args=\@ne
    \loop\ifnum\equi@args<\equi@columns
      \advance\equi@args by \@ne
      \equi@addto@colspec{@{\extracolsep{\fill}}c}%
    \repeat
    \equi@addto@colspec{@{}}%
    \equi@currow=0
    \def\equi@body{}%
    \loop
      \advance\equi@currow by \@ne
      \expandafter\equi@addto@body@expand\expandafter
        {\csname equi@row@\the\equi@currow\endcsname}%
    \ifnum\equi@rows>\equi@currow
      \equi@addto@body{\\}%
    \repeat
    \begin{tabular*}{\textwidth}{\equi@colspec}%
      \equi@body
    \end{tabular*}%
  }
\newcommand\equi@addto@body[1]
  {\edef\equi@body{\unexpanded\expandafter{\equi@body#1}}}
\newcommand\equi@addto@body@expand[1]
  {\expandafter\equi@addto@body\expandafter{#1}}
\newcommand\equi@format[1]
  {\@for\equi@tmp:=#1\do{\equi@tmp\,}}
\makeatother

\begin{document}
\equispace<5>[96]
  {a,b,c}{d,e,f}{g,h}{i,j,k,l}{m,n,o}{p,q,r,s,t}{u,v}{w}{x,y}{z}{1,2}{3,4}
  {a,b,c}{d,e,f}{g,h}{i,j,k,l}{m,n,o}{p,q,r,s,t}{u,v}{w}{x,y}{z}{1,2}{3,4}
  {a,b,c}{d,e,f}{g,h}{i,j,k,l}{m,n,o}{p,q,r,s,t}{u,v}{w}{x,y}{z}{1,2}{3,4}
  {a,b,c}{d,e,f}{g,h}{i,j,k,l}{m,n,o}{p,q,r,s,t}{u,v}{w}{x,y}{z}{1,2}{3,4}
  {a,b,c}{d,e,f}{g,h}{i,j,k,l}{m,n,o}{p,q,r,s,t}{u,v}{w}{x,y}{z}{1,2}{3,4}
  {a,b,c}{d,e,f}{g,h}{i,j,k,l}{m,n,o}{p,q,r,s,t}{u,v}{w}{x,y}{z}{1,2}{3,4}
  {a,b,c}{d,e,f}{g,h}{i,j,k,l}{m,n,o}{p,q,r,s,t}{u,v}{w}{x,y}{z}{1,2}{3,4}
  {a,b,c}{d,e,f}{g,h}{i,j,k,l}{m,n,o}{p,q,r,s,t}{u,v}{w}{x,y}{z}{1,2}{3,4}
\end{document}

该命令\equispace接受两个可选参数,第一个参数<>是列数,默认为 3,第二个参数[]是参数数,默认为 6。

输出 5 列和 96 个参数: 在此处输入图片描述

答案3

listofitems来救援!

使用语法:\equispace{a,b,c/d,e,f/g,h/i,j,k,l/m,n,o/p,q,r,s,t}

\documentclass{article}
\usepackage{listofitems,stackengine,pgffor}
\newcommand\mybox{{\fboxsep=-\fboxrule\fbox{\strut\rule{\linewidth}{0pt}}}}
\newcommand\equispace[1]{%
  \setsepchar[;]{/;,}%
  \readlist*\mylist{#1}%
  \noindent\leavevmode\rlap{\stackunder[-\fboxrule]{\mybox}{\mybox}}%
  ~\foreach\x in{1,3,...,\listlen\mylist[]}{%
    \ifnum\x=1\relax\else\hfill\fi%
    \def\stacktype{L}%
    \stackunder{\foreachitem\y\in\mylist[\x]{\ifnum\ycnt=1\else\ \fi\y}}%
      {\foreachitem\y\in\mylist[\numexpr\x+1]{\ifnum\ycnt=1\else\ \fi\y}}%
  }~%
}
\begin{document}
\equispace{a,b,c/d,e,f/g,h/i,j,k,l/m,n,o/p,q,r,s,t}
\end{document}

在此处输入图片描述

相关内容