我有一系列表格,这些表格非常(非常!)宽,但只有 3 行。有什么方法可以让表格在页边距内换行,而无需在文件.tex
本身内拆分行?
答案1
\documentclass{article}
\usepackage{array}
\newbox\zz
\makeatletter
\newenvironment{wraptable}
{\def\endarray{\crcr \egroup
\global\setbox1\lastbox
\unskip\global\setbox3\lastbox
\unskip\global\setbox5\lastbox
\egroup \@arrayright \gdef\@preamble{}}%
\setbox0\hbox\bgroup\tabular}
{\endtabular\egroup
\setbox0\hbox{}
\loop
\ifdim\wd1=0pt
{\raggedright\unhbox0\par}%
\else
\global\setbox1\hbox{\unhbox1 \unskip\global\setbox7\lastbox}%
\global\setbox3\hbox{\unhbox3 \unskip\global\setbox9\lastbox}%
\global\setbox5\hbox{\unhbox5 \unskip\global\setbox\zz\lastbox}%
\setbox0\hbox{\vbox{\box\zz\box9\box7\kern10pt}\penalty0\unhbox0}%
\repeat
}
\makeatother
\begin{document}
\begin{wraptable}{*{26}{l}}
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&1&2&3&1&2&3&1&2&3&1&2&3&1&2&3&1&2&3&1&2&3&1&2\\
one&two&three&one&two&three&one&two&three&one&two&three&
one&two&three&one&two&three&one&two&three&one&two&three&
one&two
\end{wraptable}
\end{document}
答案2
不容易。但有办法解决这个问题,避免重复你的表结构(并促进一致性)。下面的例子使用了删除列的最简单方法?有选择地隐藏表中的某些列:
\documentclass{article}
\usepackage{array,tabularx}% http://ctan.org/pkg/{array,tabularx}
\newcolumntype{H}{>{\setbox0=\hbox\bgroup}c<{\egroup}@{}}
\newcommand{\mywidetable}{%
one & two & three & four & five & six & seven & eight & nine & ten \\
1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10
}
\newcommand{\insertmywidetable}[1]{%
\begin{tabular}{#1}\mywidetable\end{tabular}}
\newcommand{\insertmywidetablex}[1]{%
\begin{tabularx}{\linewidth}{#1}\mywidetable\end{tabularx}}
\setlength{\parindent}{0pt}% Just for this example
\begin{document}
\insertmywidetable{*{10}{c}}
\hrulefill
\insertmywidetable{ccc*{7}{H}}\par
\insertmywidetable{HHHcccHHHH}\par
\insertmywidetable{*{6}{H}cccc}
\hrulefill
\insertmywidetablex{XXX*{7}{H}}\par
\insertmywidetablex{HHHXXXHHHH}\par
\insertmywidetablex{*{6}{H}XXXX}
\end{document}
您可以将表格内容存储在宏中(例如\mywidetable
),然后创建一个表格设置宏\insertmywidetable
。后者接受列规范的参数,您可以选择性地使用H
隐藏特定列或其他内容(X
如果使用tabularx
,比如说,或者c
,或者...)。
显示了两个示例,首先使用传统的tabular
with c
-columns,然后使用tabularx
with X
-columns。