朋友们,我正在尝试生成一个表格,其中所有列的间距都相等(宽度相同)。 在我的例子中,所有列的宽度将是表格中最宽列的宽度:
\documentclass{article}
\begin{document}
\begin{tabular}{ll}
a & b \\
c & Mary had a little lamb
\end{tabular}
\end{document}
有什么提示吗?:)
答案1
好吧,我之前一直在解决这个问题,最后得到了一个相当通用的解决方案,请看示例(您需要运行代码两次才能获得正确的结果):
\documentclass{article}
\usepackage{array}
\makeatletter
\newlength\eqcol@newlen
\newlength\eqcol@oldlen
\let\eqcol@bc\hfil
\let\eqcol@ec\hfil
\let\eqcol@br\hfil
\let\eqcol@el\hfil
\newcolumntype{e}[1]{%
>{\setbox0\hbox\bgroup}#1%
<{\egroup
\ifdim\wd0<\eqcol@newlen\else\global\eqcol@newlen\wd0\fi
\ifdim\wd0<\eqcol@oldlen\else\global\eqcol@oldlen\wd0\fi
\hbox to \eqcol@oldlen{%
\csname eqcol@b#1\endcsname
\box0 %
\csname eqcol@e#1\endcsname
}%
}%
}
\newcount\eqcol@count
\def\eqcolRead{%
\global\advance\eqcol@count1 %
\eqcol@oldlen5em\relax
\csname eqcol@def@\romannumeral\eqcol@count\endcsname
}
\def\eqcolWrite{%
\immediate\write\@auxout{%
\gdef\expandafter\noexpand\csname eqcol@def@\romannumeral\eqcol@count\endcsname
{\global\eqcol@oldlen\the\eqcol@newlen\relax}%
}%
\global\eqcol@newlen0pt\relax
}
\let\eqcol@old@tabular\tabular
\def\tabular{\eqcolRead\eqcol@old@tabular}
\let\eqcol@old@endtabular\endtabular
\def\endtabular{\eqcol@old@endtabular\eqcolWrite}
\makeatother
\begin{document}
\begin{tabular}{|el|er|}
\hline
111 & 22 \\\hline
33 & 4444444 \\\hline
\end{tabular}
\begin{tabular}{|ec|er|}
\hline
1 & 22 \\\hline
3 & 4 \\\hline
\end{tabular}
\begin{tabular}{|el|er|}
\hline
111 & 22 \\\hline
33 & 444444 \\\hline
\end{tabular}
\[
A =
\eqcolRead\begin{array}{*3{ec}}
1 & 10000 & 1 \\ 1 & 1 & 1
\end{array}\eqcolWrite
\]
\end{document}
环境tabular
默认经过调整,在其他情况下,您必须手动进行调整(参见array
上面的示例)。
答案2
这是受到 tohecz 启发的,但使用专用环境,我认为这更好。
在 中,equalizedtabular
可以使用任何列说明符,而不仅仅是L
、C
或R
,但当然只有这些才会相等。由于.aux
使用了文件,因此需要两次传递,并且在两次传递之间包含 时,equalizedtabular
第一次编译将产生错误的结果;但是,下一次运行 LaTeX 将修复长度。
\documentclass{article}
\usepackage{array,collcell}
\newcolumntype{C}{>{\collectcell\docellC}c<{\endcollectcell}}
\newcolumntype{L}{>{\collectcell\docellL}c<{\endcollectcell}}
\newcolumntype{R}{>{\collectcell\docellR}c<{\endcollectcell}}
\makeatletter
\providecommand{\@nameedef}[1]{\expandafter\edef\csname#1\endcsname}
\newcommand{\docell}[2]{%
\sbox\equalizedtablebox{#2}%
\ifdim\wd\equalizedtablebox>\@nameuse{finallen\theequalizedtable}\relax
\global\@nameedef{finallen\theequalizedtable}{\the\wd\equalizedtablebox}%
\fi
\makebox[\@nameuse{startinglen\theequalizedtable}][#1]{#2}%
}
\newcommand{\docellC}[1]{\docell{c}{#1}}
\newcommand{\docellL}[1]{\docell{l}{#1}}
\newcommand{\docellR}[1]{\docell{r}{#1}}
\newcounter{equalizedtable}
\newsavebox\equalizedtablebox
\newenvironment{equalizedtabular}[2][c]
{%
\stepcounter{equalizedtable}%
\global\@namedef{finallen\theequalizedtable}{0pt}%
\@ifundefined{startinglen\theequalizedtable}
{\@namedef{startinglen\theequalizedtable}{5em}}{}
\tabular[#1]{#2}%
}
{%
\endtabular
\begingroup\edef\x{\endgroup
\write\@auxout{%
\global\noexpand\noexpand\noexpand\@namedef{startinglen\theequalizedtable}%
{\@nameuse{finallen\theequalizedtable}}%
}}\x
}
\makeatother
\begin{document}
\begin{equalizedtabular}{|L|C|}
\hline
111 & 22 \\\hline
33 & 4444444 \\\hline
\end{equalizedtabular}
\begin{equalizedtabular}{|R|C|}
\hline
111 & 22 \\\hline
33 & 4 \\\hline
\end{equalizedtabular}
\end{document}
答案3
我会稍微改变一下输入语法:
\documentclass{article}
\makeatletter
\def\paulo#1{\count@#1\relax\leavevmode\setbox\z@\vbox\bgroup}
\def\endpaulo{\egroup\dimen@\wd\z@\global\setbox\@ne\null
\setbox\z@\vbox{\unvbox\z@\loop\unskip\unskip\setbox\z@\lastbox
\ifvoid\z@\else
\global\setbox\@ne\hbox{\hbox to\dimen@{\box\z@\hfil}\penalty\z@\unhbox\@ne}%
\repeat}%
\vbox{\hsize\count@\dimen@\noindent\unhbox\@ne}}
\def\cell#1{\hbox{\kern\tabcolsep\ignorespaces#1\unskip\kern\tabcolsep}}
\makeatother
\begin{document}
\begin{paulo}{2}
\cell{a}
\cell{b}
\cell{c}
\cell{Mary had a little lamb}
\end{paulo}
\end{document}
答案4
这个答案与 egreg 的答案有点相似回答还有很多来自等式包裹。
\documentclass{article}
\usepackage{array}
\usepackage{eqparbox}
\makeatletter
\newcounter{eqtabular}
\newsavebox\eqtabularbox
\newcolumntype{C}{>{\begin{lrbox}{\eqtabularbox}}c<{\end{lrbox}%
\eqmakebox [\the\c@eqtabular @eqtabular]%
{\unhcopy\eqtabularbox}}}
\newcolumntype{L}{>{\begin{lrbox}{\eqtabularbox}}c<{\end{lrbox}%
\eqmakebox [\the\c@eqtabular @eqtabular][l]%
{\unhcopy\eqtabularbox}}}
\newcolumntype{R}{>{\begin{lrbox}{\eqtabularbox}}c<{\end{lrbox}%
\eqmakebox [\the\c@eqtabular @eqtabular][r]%
{\unhcopy\eqtabularbox}}}
\newenvironment{eqtabular}[2][c]
{%
\stepcounter{eqtabular}%
\tabular[#1]{#2}%
}
{%
\endtabular
}
\makeatother
\begin{document}\thispagestyle{empty}
\begin{eqtabular}{|L|L|}
\hline
111 & 22 \\\hline
33 & 4444444 \\\hline
\end{eqtabular}
\begin{eqtabular}{|R|R|}
\hline
111 & 22 \\\hline
33 & 4444444 \\\hline
\end{eqtabular}
\begin{eqtabular}{|C|C|}
\hline
11111111111 & 22 \\\hline
33 & 4444444 \\\hline
\end{eqtabular}
\begin{eqtabular}{|R|L|}
\hline
11111111111 & 22 \\\hline
33 & 4444444 \\\hline
\end{eqtabular}
\begin{eqtabular}{|R|C|}
\hline
111 & 22 \\\hline
33 & 4 \\\hline
\end{eqtabular}
\begin{eqtabular}{|L|R|}
\hline
111 & 22 \\\hline
33 & 4 \\\hline
\end{eqtabular}
\end{document}