1

Is there any way of typesetting the % symbol in \verb within a tabularx-cell?

\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\begin{document}

    \verb|b%a|

    \begin{tabular}{l l}
        \verb|b%a| & a
    \end{tabular}

    \begin{tabularx}{\linewidth}{l X}
        \verb|b%a| & a \\
    \end{tabularx}

\end{document}
5
  • In tabularx the body of the environment is already read and therefore tokenised. The category code changes of \verb can't be applied inside of it. Therefore, you'd have to change the catcode of % before the \begin{tabularx}, this means that you can't put comments there, too. You could do so by putting \begingroup\catcode`\%=12 prior to the \begin{tabularx} and \endgroup after \end{tabularx}.
    – Skillmon
    Jul 8, 2019 at 15:50
  • 3
    Footnote on page 2 of the manual for tabularx: Since Version 1.02, \verb and \verb* may be used, but they may treat spaces incorrectly, and the argument can not contain an unmatched { or }, or a % character.
    – egreg
    Jul 8, 2019 at 16:01
  • why not \texttt{b\%a} why \verb ? Jul 8, 2019 at 16:28
  • I opted for \verb to avoid the need of escaping all those %s etc. in the code code extracts that I was pasting, but you're right of course, I did end up using \ţexttt then.
    – Bastian
    Jul 18, 2019 at 16:28
  • @egreg you're right of course, I should have checked the manual first
    – Bastian
    Jul 18, 2019 at 16:29

1 Answer 1

1

Turning my comment into an answer: If you don't need comments inside of the body of tabularx you can turn the % char into a character which can be input directly. If you need comments you can turn another character which you don't need there into the comment char with

\category`\<char>=14

Just replace <char> with a single character which should be the comment character. In the example below I used # for the comments inside of tabularx:

\documentclass[]{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\begin{document}

    \verb|b%a|

    \begin{tabular}{l l}
        \verb|b%a| & a
    \end{tabular}

    \begingroup
    % use `%' as a normal character and `#` as comment character
    \catcode`\#=14
    \catcode`\%=12
    \begin{tabularx}{\linewidth}{l X}
        \verb|b%a| & a \\ # this is a comment
    \end{tabularx}
    # \endgroup ends the scope of our category changes, so afterwards `#' is
    # again the parameter character and `%' the comments character
    \endgroup

\end{document}

enter image description here

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.