2

I want to define a table as command. Some lines schould only be printed, if there is a value for it. Because of that I am using the \ifthenelse command.

Example:

\begin{tabularx}{\linewidth}{|l|X|}
        \hline
        Titel & #1\\ \hline
        Caption & #2\\ \hline
        \ifthenelse{\equal{#3}{}}{}{Priority & #3\\ \hline}
        \ifthenelse{\equal{#4}{}}{}{Members & #4\\ \hline}
\end{tabularx}

But there is a problem: If I leve the seccond brackets of \ifthenelse empty, a space symbol i printed. Normaly you wouldn't see it. But the tabularx knows that there is a symbol and creates a new line for this symbols. Both packages ifthen and xifthen have this problem.

enter image description here

How to avoid, that \ifthenelse creates the space symbol?

2

There are better alternatives to \ifthenelse that don't have the problem.

Instead of \ifthenelse{\equal{#3}{}}{<empty>}{<not empty>} you can use

\ifstrempty{#3}{}{Priority & #3\\ \hline}
\ifstrempty{{#4}}{}{Members & #4\\ \hline}

This needs \usepackage{etoolbox}.

Another possibility:

\documentclass{article}
\usepackage{xparse}
\usepackage{tabularx}

% see https://tex.stackexchange.com/a/467527/4427
\ExplSyntaxOn
\NewExpandableDocumentCommand{\xifthenelse}{mmm}
 {
  \bool_if:nTF { #1 } { #2 } { #3 }
 }

\cs_new_eq:NN \numtest \int_compare_p:n
\cs_new_eq:NN \oddtest \int_if_odd_p:n
\cs_new_eq:NN \dimtest \dim_compare_p:n
\cs_new_eq:NN \deftest \cs_if_exist_p:N
\cs_new_eq:NN \namedeftest \cs_if_exist_p:c
\cs_new_eq:NN \eqdeftest \token_if_eq_meaning_p:NN
\cs_new_eq:NN \streqtest \str_if_eq_p:ee
\cs_new_eq:NN \emptytest \tl_if_blank_p:n
\prg_new_conditional:Nnn \xxifthen_legacy_conditional:n { p,T,F,TF }
 {
  \use:c { if#1 } \prg_return_true: \else: \prg_return_false: \fi:
 }
\cs_new_eq:NN \boolean \xxifthen_legacy_conditional_p:n
\ExplSyntaxOff

\newcommand{\ahrtable}[4]{%
  \par\noindent
  \begin{tabularx}{\linewidth}{|l|X|}
    \hline
    Title & #1\\ \hline
    Caption & #2\\ \hline
    \xifthenelse{\emptytest{#3}}{}{Priority & #3\\ \hline}
    \xifthenelse{\emptytest{#4}}{}{Members & #4\\ \hline}
  \end{tabularx}%
}


\begin{document}

\ahrtable{Title}{Caption}{}{}

\bigskip

\ahrtable{Title}{Caption}{Priority}{}

\bigskip

\ahrtable{Title}{Caption}{}{Members}

\bigskip

\ahrtable{Title}{Caption}{Priority}{Members}

\end{document}

enter image description here

3

as no test document was supplied this is untested but basically you can do the tests before ending the previous line:

\begin{tabularx}{\linewidth}{|l|X|}
        \hline
        Titel & #1\\ \hline
        Caption & #2%
        \ifthenelse{\equal{#3}{}}{}{\\ \hline Priority & #3}%
        \ifthenelse{\equal{#4}{}}{}{\\ \hline Members & #4}\\ \hline
\end{tabularx}
  • Yes it works if you do it like this. Thank you. But why does \ithenelse adds an space to an empty block? – Ahrtaler Apr 11 at 15:30
  • 1
    @Ahrtaler it doesn't it is that if you use anything there (try \relax or {} for example) the tex starts processing a new cell in a new last line – David Carlisle Apr 11 at 15:33
1

I'm not entirely sure why the \ifthenelse expansion creates extra spaces on output, but it seems to be related to the fact that it isn't fully expandable, i.e. uses assignments internally.

When you define a similar command \ifequal that actually does expand completely, the extra output is gone:

\documentclass{article}
\usepackage{tabularx}
\usepackage{xifthen}

\newcommand\ifequal[4]{\ifnum\pdfstrcmp{#1}{#2}=0 #3\else #4\fi}

\parindent=0pt
\begin{document}
\begin{tabularx}{\linewidth}{|l|X|}
        \hline
        Titel & 1\\ \hline
        Caption & 2\\ \hline
        \ifequal{}{}{}{Priority & 3\\ \hline}
        \ifequal{}{}{}{Members & 4\\ \hline}
\end{tabularx}

\begin{tabularx}{\linewidth}{|l|X|}
        \hline
        Titel & 1\\ \hline
        Caption & 2\\ \hline
        \ifequal{}{}{}{Priority & 3\\ \hline}
        \ifequal{y}{}{}{Members & 4\\ \hline}
\end{tabularx}
\end{document}

enter image description here

The disadvantage of this approach is that you need to define several specialized commands for the various kinds of conditions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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