2

It was hard for me to find a proper title (feel free to change it) for my question which I want to introduce with an example:

Situation

In my document I sometimes use tabus directly in the document without nesting them in a table-environment.

\documentclass[a4paper, 11pt]{scrbook}
\usepackage{blindtext}

\usepackage{tabu, booktabs}

\begin{document}
\section{TEST}
\blindtext
\\ \\
This is how I want my table to look like:

\ \\
{\centering
\begin{tabu} to 0.9\textwidth {X[l,2] X[l,2] X[l,3]}    \toprule \rowfont{\bfseries}
Test1   &   Test2   &   Test3   \\ \midrule
Test1   &   Test2   &   Test3   \\
Test1   &   Test2   &   Test3   \\ \bottomrule
\end{tabu}\par} \medskip \ \\
\blindtext
\end{document}

Now, I wanted to create a new environment to help me building this table and I tried something like this:

\newenvironment{texttab}[1]
    {\ \\ \centering%
    \begin{tabu} to 0.9\textwidth {#1} \toprule \rowfont{\bfseries}}%
    {\end{tabu}\par \medskip \ \\}

where #1 gives the tabu-specifications of course. It kind of works, but if you run the following MWE (adding and testing the new environment)

\documentclass[a4paper, 11pt]{scrbook}
\usepackage{blindtext}

\usepackage{tabu, booktabs}

\newenvironment{texttab}[1]
    {\ \\ \centering%
    \begin{tabu} to 0.9\textwidth {#1} \toprule \rowfont{\bfseries}}
    {\end{tabu}\par \medskip \ \\}

\begin{document}
\section{TEST}
\blindtext
\\ \\
This is how I want my table to look like:          % no new environment

\ \\
{\centering
\begin{tabu} to 0.9\textwidth {X[l,2] X[l,2] X[l,3]}    \toprule \rowfont{\bfseries}
Test1   &   Test2   &   Test3   \\ \midrule
Test1   &   Test2   &   Test3   \\
Test1   &   Test2   &   Test3   \\ \bottomrule
\end{tabu}\par} \medskip \ \\
\blindtext

\begin{texttab}{X[l,2] X[l,2] X[l,3]}              % WITH NEW ENVIRONMENT
Test1   &   Test2   &   Test3   \\ \midrule
Test1   &   Test2   &   Test3   \\
Test1   &   Test2   &   Test3   \\ \bottomrule
\end{texttab}
\blindtext
\end{document}

you will see that there is an indentation at the begin of the blindtext after the second table. Even adding \noindent into the new environment like this

\newenvironment{texttab}[1]
    {\ \\ \centering%
    \begin{tabu} to 0.9\textwidth {#1} \toprule \rowfont{\bfseries}}
    {\end{tabu}\par \medskip \ \\ \noindent}         % added \noindent here

doesn't help as it still leaves some space (which I could remove with \!\!, but...).

This wouldn't be a problem if I could use the { and } that enclose {\centering and \par} in the very first code example. However, I cannot put them into the definition of the new environment as this will cause delimiter-problems... I don't want to use the center-environment due to unwanted vertical spaces (which seems kinda hilarious at this point of writing...)

\newenvironment{texttab}[1]
    {{\\centering%               % } missing!
    \begin{tabu} to 0.9\textwidth {#1} \toprule \rowfont{\bfseries}}%
    {\end{tabu}\par} \medskip \ \\}      % { missing!

Questions

  1. Is there a way to, either, get rid of those "uncontrollable spaces" that appear in the running version, or, make the last piece of code workable? (I don't want to use a minipage here, as I maybe want to make it breakable with longtabu.)
  2. Then, sticking with the (for now) working "new environment": Why doesn't

    \newenvironment{texttab}[2]
        {\ \\ \centering%
        \begin{#1tabu} to 0.9\textwidth {#2} \toprule \rowfont{\bfseries}}
        {\end{#1tabu}\par \medskip \ \\}
    

    work, so that I could easily change the tabu into a longtabu by setting the first argument long?

  3. Should I rather try to pack all this into a new command with several arguments?

  4. Do you think that maybe the whole idea is...stupid?

1
  • You should, first of all, avoid the clumsy \ \\ construction for leaving vertical space; \par\medskip is surely better.
    – egreg
    Commented May 18, 2013 at 16:58

1 Answer 1

2

I think that keeping it simple is better. ;-)

\documentclass[a4paper, 11pt]{scrbook}
\usepackage{blindtext}

\usepackage{tabu, booktabs}

\newenvironment{texttab}[1]
  {\setlength{\topsep}{\baselineskip}%
   \begin{center}
   \begin{tabu} to 0.9\textwidth {#1} \toprule \rowfont{\bfseries}}
  {\end{tabu}\end{center}}

\begin{document}
\section{TEST}
This is how I want my table to look like:          % no new environment
\begin{center}
\begin{tabu} to 0.9\textwidth {X[l,2] X[l,2] X[l,3]}    \toprule \rowfont{\bfseries}
Test1   &   Test2   &   Test3   \\ \midrule
Test1   &   Test2   &   Test3   \\
Test1   &   Test2   &   Test3   \\ \bottomrule
\end{tabu}
\end{center}
\blindtext
\begin{texttab}{X[l,2] X[l,2] X[l,3]}              % WITH NEW ENVIRONMENT
Test1   &   Test2   &   Test3   \\ \midrule
Test1   &   Test2   &   Test3   \\
Test1   &   Test2   &   Test3   \\ \bottomrule
\end{texttab}
\blindtext
\end{document}

In order to control the vertical spacing above and below the table, modify the value of \topsep, rather than using \\ (which should never be used to “leave blank lines”). I used \topsep=\baselineskip, change it to suit your needs; I'd not even set it, to be honest, so using the default value.

enter image description here

You can solve your second problem by defining

\newenvironment{texttab}[2][]
  {\def\longornot{#1}\setlength{\topsep}{\baselineskip}%
   \begin{center}
   \begin{#1tabu} to 0.9\textwidth {#2} \toprule \rowfont{\bfseries}}
  {\expandafter\end\expandafter{\longornot tabu}\end{center}}

so that you can say

\begin{texttab}[long]{X[l,2] X[l,2] X[l,3]}
...
\end{texttab}

but I'd prefer defining a different environment, say

\newenvironment{texttab*}[1]
  {\setlength{\topsep}{\baselineskip}%
   \begin{center}
   \begin{longtabu} to 0.9\textwidth {#1} \toprule \rowfont{\bfseries}}
  {\end{longtabu}\end{center}}

The problem was that you can't use #1 in the final argument to \newenvironment. Take your pick.

4
  • Thank you for the topsep-command - I don't know why I didn't stumble over that so far... (It does affect the space above and below the environment, right?) | ad Nr. 1: I just found out that the delimiter-problem could very well be solved with \begingroup and \endgroup, is that right? | Would \centering be in any way preferable to center in this particular case (as I use \centering in table-environments)? | If you have any idea about the question Nr. 2 I'd like to accept your answer!
    – LCsa
    Commented May 18, 2013 at 19:30
  • @LCsa You could solve question #2, in an indirect way, but I suggest you to use a different environment, such as texttab*. I'll add it.
    – egreg
    Commented May 18, 2013 at 20:38
  • Thank you, @egreg, I will now try to understand the solution you provided, but it's...hard for me. xD Is there a special reason why you recommend using a different environment?
    – LCsa
    Commented May 18, 2013 at 21:33
  • May I link here to a follow-up-question? Thanks. Vertical Spaces above and below longtabu
    – LCsa
    Commented May 21, 2013 at 8:22

You must log in to answer this question.

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