I would like to define a new environment with conditions in my template file. My environment consists in a lign of header filled by a title (given at the environment in an argument) and then at the next line the body of the text provided between the \begin{...} and \end{...}

More specifically I would like, given an argument of the environment, to put the title + body in a tabular to handle its position. The argument is a number, if the number equals 0, the title + body are normally defined. On the other hand, if the number differs from 0, the title+body are defined in a tabular with a unique cell that contains both.

Below is a solution that actually works but I face error messages (though it compiles) :

{\noindent\ifthenelse{ \equal{#1}{0} }{


I used the package ifthen, and I think that is the only one required for this.

Some cases of utilisation below :

Body of the text without tabular\\

Body of the text with tabular\\

The output of this code is the following :

enter image description here

So the code here actually works but I have several errors like "Extra }, or forgotten \endgroup.". I would like to know if someone could find my error here?

NB : as you may understand from this example, I ultimately try to put a defined number of vertical side lines on the left of my text. If you have a better approach than the one I chose, I am open to any suggestion!

  • 1
    I have no idea, what exactly you are trying to achieve, but if it is some kind of box, you'd be better off using something similar to ctan.org/pkg/tcolorbox
    – DG'
    Commented Apr 16, 2021 at 11:10

1 Answer 1


The biggest problem is that you always use \end{tabular} even if the code didn't use a tabular in the begin part. You could define an auxiliary macro to store whether you should use \end{tabular} or not, like in the following. Also please note that your code does include many unwanted spaces. Make sure to put a % at the end of lines to suppress spaces there, e.g., use {% instead of just { in your \ifthenelse blocks.

You could use the optional argument to define how many rules should be placed in the tabular, the following does so using the *{<num>}{<toks>} syntax of the tabular preamble.

I've also changed some of your \\ to use \par instead, made sure that the environment will always start on a line of its own (by putting \par before \noindent), suppress the indentation of the first line in the environment body using \@afterindentfalse\@afterheading (a mechanism LaTeX has defined for its sectioning titles), and suppress the indentation of the paragraph following the environment using \@endpetrue, this way, if you put a blank line between \end{test} and the following text the paragraph will be indented, if you don't put a blank line there the text will not be indented (this is used by LaTeX's lists).

Note that the code doesn't change the width of your table dynamically based on how many vertical rules you use, as a result you can get an overfull hbox warning if you use too many rules. Leave me a comment if you'd like that to be changed.

Here is the code:



\newcommand*\testEND{} % initialise the macro to check that it doesn't conflict

\begin{test}{Something important}
  Body of the text without tabular.
Text outside the environment
\begin{test}[1]{Something important}
  Body of the text with tabular.

\begin{test}[2]{Something important}
  Body of the text with tabular and two lines.

\begin{test}[20]{Something important}
  Body of the text with tabular and two lines.

Text outside the environment

enter image description here

You must log in to answer this question.

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