0

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) :

\newenvironment{test}[2][0]
{\noindent\ifthenelse{ \equal{#1}{0} }{
{#2}
    
}{

\begin{tabular}{|p{0.9\textwidth}}
{#2}\\
}}
{ 
\\
\end{tabular} 
}

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

Some cases of utilisation below :

\begin{test}[0]{Title}
Body of the text without tabular\\
\end{test}

\begin{test}[1]{Title}
Body of the text with tabular\\
\end{test}

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
  • 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'
    Apr 16, 2021 at 11:10

1 Answer 1

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:

\documentclass[]{article}

\usepackage{ifthen}

\makeatletter
\newcommand*\testEND{} % initialise the macro to check that it doesn't conflict
\newenvironment{test}[2][0]
  {%
    \par
    \noindent
    \ifthenelse{\equal{#1}{0}}
      {%
        \def\testEND{}%
        #2\par
        \@afterindentfalse\@afterheading
      }
      {%
        \def\testEND{\end{tabular}}%
        \begin{tabular}[t]{*{#1}{|}p{.9\linewidth}@{}}%
        #2\\%
      }%
  }
  {%
    \testEND
    \par\@endpetrue
  }
\makeatother

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

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

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

Text outside the environment
\end{document}

enter image description here

You must log in to answer this question.

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