4

I'm trying to both start and end a table with \newcommand. I'd like to switch to using \EndTableSetupFourColumns (as it removes several warnings and is easier to understand for other readers) but for some reason LaTeX does not seem to recognize the \end{tabu} found within, so I have to insert it manually as seen below.

This problem seems to be limited to tabu - when I was using tabulars I did not have this issue. I looked through the tabu documentation and could not find anything that would indicate this wouldn't work.

Looking at \end{tabu} in \newcommand shows that it won't work, however, it is something I'd like to work around for code readability, if nothing else.

MWE:

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{longtable,tabu}

\newcommand{\TableSetupFourColumns}{%
\rowcolors{2}{gray!25}{white}
\begin{tabu} to \textwidth {lccX}
    \rowcolor{gray!50}}

\newcommand{\EndTableSetupFourColumns}{%
\end{tabu}}

\begin{document}

\section{Test 1}
This is text.\par \noindent
\TableSetupFourColumns
    Test            & test  & test  & test\\
\end{tabu}
%\EndTableSetupFourColumns

\end{document}

1 Answer 1

6

tabu needs to "see" \end{tabu}; it cannot be hidden in a macro. You're better served by defining a new environment:

\documentclass{article}

\usepackage[table]{xcolor}
\usepackage{longtable,tabu}

\newenvironment{tabu4}
  {\rowcolors{2}{gray!25}{white}
   \begin{tabu} to \textwidth {lccX}
   \rowcolor{gray!50}}
  {\end{tabu}}

\begin{document}

\section{Test 1}
This is text.

\medskip

\noindent
\begin{tabu4}
    Test            & test  & test  & test\\
    Test            & test  & test  & test\\
    Test            & test  & test  & test\\
    Test            & test  & test  & test\\
    Test            & test  & test  & test\\
    Test            & test  & test  & test\\
\end{tabu4}

\end{document}

enter image description here

6
  • Thanks, that's exactly what I was looking for. However, when I perform the same with longtabu I'm getting a Misplaced \cr error at each \end tag. I'll see if I can get a MNWE of that up. Apr 23, 2013 at 20:43
  • 1
    @LearningLaTeX I'm afraid that longtabu is a different matter.
    – egreg
    Apr 23, 2013 at 20:44
  • Well that would explain my issue. Can I not perform this with longtabu at all then? Would using \newcommand work for longtabu, or could I get it working with \newenvironment? Apr 23, 2013 at 20:49
  • @LearningLaTeX If in the example I change tabu4 to longtabu4 and tabu in the definition to longtabu, I don't have any problem.
    – egreg
    Apr 23, 2013 at 20:51
  • Yeah, it seems to be an interaction between my fancyhdr's \fancyhead and \fancyfoot when the table is big enough to span more than one page. I've got a MNWE that I'll post if I can't figure out how to fix it myself. Apr 24, 2013 at 13:54

You must log in to answer this question.

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