9

I have a series of tables that are going to be very (very!) wide, but with only 3 rows. Is there any way I can get the tables to wrap within my margins, without having to break the rows within the .tex file itself?

3
  • Welcome to TeX.sx No need to addd thanks, simply upvote good answers to your Q that you receive. Feb 7, 2013 at 20:14
  • Please provide a bit more information about the tables' structure. E.g., how many columns do they contain, and what is the content of each cell: Simple integers, decimal numbers, text, images, or a mixture of all of the above?
    – Mico
    Feb 7, 2013 at 20:15
  • 1
    Outside-of-the-box answer: You could flip the orientation of the table and make it a three-column table that wraps vertically .In order to make better use of the horizontal space then, you could use a multicols environment from the multicol package. Let me know if that’s an option, I can add a sample.
    – doncherry
    Feb 7, 2013 at 20:24

2 Answers 2

11

enter image description here

\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}
3
  • Works great, but this breaks the use of \hline it seems. Is there any other way with this macro to make a horizontal line? Or a different solution which does allow horizontal lines?
    – PHPirate
    Jan 1, 2018 at 16:31
  • best not to ask a question on a comment on such an old answer, you could ask a new question referencing this, I'm not sure if I can guess how you would want horizontal lines to work if the cells are wrapping. So a new question would give you space to make a clearer example @PHPirate Jan 1, 2018 at 17:01
  • Yeah sorry my bad, will do.
    – PHPirate
    Jan 1, 2018 at 17:02
5

Not easily. But there are ways around this, to avoid duplicating your table structure (and promote consistency). The example below uses the idea from Easiest way to delete a column? to selectively hide certain columns in a table:

enter image description here

\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}

You would store the contents of the table in a macro (say \mywidetable) and then create a table-setting macro \insertmywidetable. The latter takes an argument for the column specification, where you selectively use H to hide that particular column, or something else (X if using tabularx, say, or c, or...).

Two examples are shown, first using a traditional tabular with c-columns, then using a tabularx with X-columns.

3
  • Starting an answer with "No" That's just setting a challenge:-) Feb 7, 2013 at 21:16
  • @DavidCarlisle: Well well, and you rose to the occasion. I'll keep my inferior attempt at showing authority with an edit... :-|
    – Werner
    Feb 7, 2013 at 21:18
  • actually I forgot to +1 yours (done now) It's probably more robust if you want to use tabularx or fancy inter-column rules or something. Feb 7, 2013 at 21:20

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .