Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to specify a table and if it has any multiline cells then it should be output as a single pair of columns and otherwise as two pairs of columns. For example, if it looks like the First Table in the code below then it should be output as is. If it looks like the Second Table in the code below then it should be output as the Third Table.

Firstly, how can I accomplish this? I tried to put the code in an \sbox but kept getting errors; however, even if I do that I don't know what the next step would be. ADDED Note that its ok to specify the data in the table twice in the latex code since I am generating this from a program.

Secondly, the code below gives rise to a ton of messages such as (\end occurred when \ifhmode on line 35 was incomplete) although it still runs. What do these messages mean? What do I do about them?

\documentclass{article}
\usepackage[top=1in,bottom=1in,left=0.5in,textwidth=7.5in]{geometry}
\usepackage{lipsum}
\usepackage{multicol}
\usepackage{tabu}
\usepackage{xcolor}
\begin{document}

First table.

\setlength{\columnseprule}{.4pt} 
\renewcommand\columnseprulecolor{\color{blue}}
\begin{multicols}{2}
\tabulinesep=4pt
\noindent
\begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut}} 
\everyrow{\tabucline[blue]-}
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
\end{tabu} 
\tabulinesep=4pt
\noindent
\begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut}} 
\everyrow{\tabucline[blue]-}
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & more / words \\ 
Some words & and some more \\ 
\end{tabu} 
\end{multicols} 

Second table.

\setlength{\columnseprule}{.4pt} 
\renewcommand\columnseprulecolor{\color{blue}}
\begin{multicols}{2}
\tabulinesep=4pt
\noindent
\begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut}} 
\everyrow{\tabucline[blue]-}
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
\end{tabu} 
\tabulinesep=4pt
\noindent
\begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut}} 
\everyrow{\tabucline[blue]-}
Some words & and some more \\ 
Some words &  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibu- lum ut, placerat ac, adipiscing vitae, felis. \\
Some words & and some more \\ 
Some words & more / words \\ 
Some words & and some more \\ 
\end{tabu} 
\end{multicols} 

Third table.

\begin{center}
\tabulinesep=4pt
\noindent
\begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut}} 
\everyrow{\tabucline[blue]-}
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words & and some more \\ 
Some words &  Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibu- lum ut, placerat ac, adipiscing vitae, felis. \\
Some words & and some more \\ 
Some words & more / words \\ 
Some words & and some more \\ 
\end{tabu} 
\end{center}

\end{document}

The result is:

enter image description here

ADDED

Note that there are some relationships to these posts:

Hide an entire table (but still generate it)

Vertical alignment in multicol

share|improve this question

1 Answer 1

I include extra printing to help explain the methodology. And done in default article (sorry, no geometry but that's not a hindrance). Since you mentioned that data is autogenerated, I ask that you write it to external file named, for this MWE, tabledata.tex, an example of which is this:

Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & and some more
Some words & more / words
Some words & and some more

I use the readarray package to read the data (into \x). But it's also stored in a row-by-row form which I grab with \copyrecords and stuff into an array structure named mydata. Because it includes a blank line at the end of the file, I subtracted one off the ROWS, but if you can write the file without the extra line, you can skip that part.

Then I go through each line of data and set it in tabu and measure its width, to determine if it is narrow or wide. I used 300pt as the threshold, but you can tune that. If any row in the table exceeded that length, the table was deemed wide.

Once the table was deemed either wide or narrow, it was merely a question of using an \if branch to set up the proper table.

\documentclass{article}
\parindent 0in
\usepackage{tabu}
\usepackage{readarray}
\usepackage{ifthen}
\usepackage[nomessages]{fp}
\usepackage{xcolor}
\usepackage{multicol}
\global\newlength\boxwidth
\newcommand\showwidth[1]{\findwidth{#1}\the\boxwidth}
\newcommand\findwidth[1]{\setbox0\hbox{#1}\setlength\boxwidth{\wd0}}
\newcounter{index}
\begin{document}
\readdef{tabledata.tex}{\x}
\copyrecords{mydata}
\setcounter{index}{\mydataROWS}
\addtocounter{index}{-1}
\edef\mydataROWS{\arabic{index}}
Data rows: \mydataROWS

Here are the measure of the data rows

\setcounter{index}{0}\def\widetable{F}
\whiledo{\value{index} < \mydataROWS}{%
  \addtocounter{index}{1}%
  \def\y{%
    \begin{tabu} to%
      \columnwidth{>{\bfseries}l>{\strut\ignorespaces}l<{\ifhmode\unskip\strut\fi}}%
      \everyrow{\tabucline[blue]-}%
      \bfseries\arrayij{mydata}{\value{index}}{1}\\%
    \end{tabu}%
  }%END DEF
  \showwidth{\y}~~\y\\%
  \findwidth{\y}
  \ifthenelse{\lengthtest{\boxwidth > 300pt}}{\def\widetable{T}}{}
}%END WHILEDO

The table is wide? \widetable

\if T\widetable
  \begin{center}
  \tabulinesep=4pt
  \noindent
  \setcounter{index}{0}
  \begin{tabu} %
    to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut\fi}} 
    \everyrow{\tabucline[blue]-}
    \whiledo{\value{index} < \mydataROWS}{%
      \addtocounter{index}{1}%
      \arrayij{mydata}{\arabic{index}}{1}%
      \ifthenelse{\equal{\arabic{index}}{\mydataROWS}}{}{\\}%
    }
  \end{tabu} 
  \end{center}
\else
  \setlength{\columnseprule}{.4pt} 
  \renewcommand\columnseprulecolor{\color{blue}}
  \FPeval{\dblrows}{trunc((\mydataROWS+1)/2,0)}
  Rows for double-column table: \dblrows
%  
  \renewcommand\columnseprulecolor{\color{blue}}
  \begin{multicols}{2}
    \tabulinesep=4pt
    \noindent
    \setcounter{index}{0}
    \begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut\fi}} 
      \everyrow{\tabucline[blue]-}
      \whiledo{\value{index} < \dblrows}{%
        \addtocounter{index}{1}
        \arrayij{mydata}{\arabic{index}}{1}%
        \ifthenelse{\equal{\arabic{index}}{\dblrows}}{}{\\}%
      }
    \end{tabu} 
    \tabulinesep=4pt
    \noindent
    \begin{tabu} to \columnwidth{>{\bfseries}l>{\strut\ignorespaces}X<{\ifhmode\unskip\strut\fi}} 
      \everyrow{\tabucline[blue]-}
      \whiledo{\value{index} < \mydataROWS}{%
        \addtocounter{index}{1}
        \arrayij{mydata}{\arabic{index}}{1}%
        \ifthenelse{\equal{\arabic{index}}{\mydataROWS}}{}{\\}%
      }
    \end{tabu} 
  \end{multicols} 
\fi
\end{document}

For a wide data example:

enter image description here

and for narrow data example:

enter image description here

p.s. To your question " What do these messages mean? What do I do about them?" Each \if must be closed with a \fi. Your \ifhmode was not closed, causing the grief.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.