Search code examples
latex

Misplaced \noalign if using \ifstrequal


I get a "misplaced \noaling" error within an tabularx environment if I am using \ifstrequal.

This is working:

\documentclass{article}

\usepackage{tabularx}
\usepackage{etoolbox}

\begin{document}

\begin{tabularx}{\textwidth}{rrrrr}
    I & am & a & useless & table \\
    \ifstrequal{a}{a}{
        I & am & a & useless & table \\
    } {
        I & am & a & useless & table \\
    }
\end{tabularx}

\end{document}

This not:

\documentclass{article}

\usepackage{tabularx}
\usepackage{etoolbox}

\begin{document}

\begin{tabularx}{\textwidth}{rrrrr}
    I & am & a & useless & table \\
    \ifstrequal{a}{a}{
        \hline
        I & am & a & useless & table \\
        \hline
    } {
        I & am & a & useless & table \\
    }
\end{tabularx}

\end{document}

Solution

  • You could resort to the good old \ifx:

    \documentclass{article}
    
    \usepackage{tabularx}
    \usepackage{booktabs}
    
    \newcommand{\stringa}{a}
    \newcommand{\stringb}{b}
    
    \begin{document}
    
    \begin{tabularx}{\textwidth}{rrrrr}
        I & am & a & useless & table \\
        \ifx\stringa\stringa
            \midrule
            I & am & a & useless & table \\
            \midrule
        \else
            I & am & a & useless & table \\
        \fi
    \end{tabularx}
    
    \end{document}
    

    (I'm using rules from the booktabs package instead of \hline because they give better spacing)