# call \pygment with # or % character in tabularx environment

I have the following MWE :

\documentclass{article}
\usepackage{pythontex}
\usepackage{tabularx}

\begin{document}

\begin{tabularx}{\linewidth}{X}
\pygment{c}{#include <stdio.h>} \\
\end{tabularx}

\end{document}


which give me

Illegal parameter number in definition of \pytx@argdetok.
Illegal parameter number in definition of \pytx@arg.
Illegal parameter number in definition of \pytx@argdetok.
Illegal parameter number in definition of \pytx@arg.
Illegal parameter number in definition of \pytx@argdetok.
Illegal parameter number in definition of \pytx@arg.


I studied a bit the behavior of \pygment{c}{#include <stdio.h>} :

1. This line works outside tabularx environment
2. If I remove # in #include, the error goes away.

Side notes :

1. Compilation : pdflatex main.tex && pythontex main.tex && pdflatex main.tex
2. It seems to be a known issue, see this and this answer of G. Poore.

You can locally make # safe:

\documentclass{article}
\usepackage{pythontex}
\usepackage{tabularx}

\begin{document}

{\catcode\#=12
\noindent\begin{tabularx}{\linewidth}{X}
\pygment{c}{#include <stdio.h>} \\
\end{tabularx}}

\end{document}

• Thanks! G. Poore said that it wouldn't be hard to get around it but I did not have enough TeX knowledge. Just a bit curious, in your opinion why this solution cannot be implemented in the package? – Firmin Martin Aug 11 '18 at 20:42
• @FirminMartin the solution is document specific, you need to move the making # safe up so it is not in the argument of any command (normally environments are Ok but tabularx is "special") but not so early that it comes before and definitions like \newcomand\foo[1]{zzz #1} which need the special # behaviour. so in your example, it can not be done when pythontex is loaded (as that would break \usepackage{tabularx}) and it can not be done by the \pygment command as that is already inside tabularx so it is too late to change #` – David Carlisle Aug 11 '18 at 21:01