I am using PHP to generate a Tex file, and am passing several variables into a template file to be rendered.

I want to create a table with several columns, but with a header that spans those columns. The header, in this case, is the name of an expense: mileage, hotel, etc.

I thought I might use the \multicolumn command for my table heading, like so:

\begin{longtabu} to \textwidth { X[2,l] X[3,l] X[1,l] X[1,l] X[1,r] X[5,l] }
\multicolumn{5}{ l }{\large{\textbf{%=expense=%}}} & \\
\textbf{Date} & \textbf{Employee} & \textbf{Quantity} & \textbf{Cost} &
    \textbf{Total} & \textbf{Notes} \tabcr
%=date=% & %=employee=% & %=qty=% & %=cost=% & %=total=% & %=note=% \tabcr

All works well, UNLESS the expense name has an ampersand in it...in which case \multicolumn gets a bit confused (maybe because it's trying to go to the next column?), as in expense = "Copies 11x17 (B&W)" Yeah, that's a no go.

My question is, can I escape that ampersand somehow? I've tried adding \verb|...| to the variable, and \texttt{...} to it. No luck. Right now I'm thinking my two options are to either get rid of the \multicolumn approach, or to forbid ampersands in expense names. Do I have another option? Is there a way to use \multicolumn?

Note: After posting, I discovered another table in our code that uses a preprocessor (in PHP) to address/escape the special characters. This is a fine enough solution for my purposes, but is there another option?

  • MWE pls - complete table! – vaettchen Aug 11 '15 at 17:08
  • 3
    In order to print &, you should use \&. – egreg Aug 11 '15 at 17:39
  • @vaettchen - A MWE would be the same table as I have now shown, but with no ampersand in the "expense" variable. Does my updated table meet MWE needs? – Bradley Mecham Aug 11 '15 at 19:02
  • Based on the comment by @egreg and the information associated with this question: how-to-put-verbatim-inside-multicolumn-in-tabular I would submit an answer that says that the ways to do it are either to pre-process the data, so that ampersands and other special characters are escaped, as egreg suggests, or to follow the example in the link. I do not have enough experience, though, to make a definitive statement. – Bradley Mecham Aug 11 '15 at 19:03
  • MWE would be everything needed but not more from \begin{document} to \end{document}, in particular the relevant \usepackage{} lines and the table scaffolding. I cannot help, unfortunately, I have never heard of `longtabu'. – vaettchen Aug 11 '15 at 20:16

If you are using any scripting language that passes strings to LaTeX, you should definitely escape them as you probably do not control all data which will be passed and your system should be fail-safe.

This means you should put a \ before any of &, #, {, } etc. which would be either causing an error or invisible in the output. If you know about other special symbols like \ you should probably also replace them with \textbackslash etc. because they could also be a problem.

Personally I would avoid using verbatim in an environment where it is absolutely overkill. Processing text should be as easy as possible. And replacing one character by a saved box would just be complicated.

Btw: The mentioned \texttt does not do anything like escaping (or switching to verbatim), it just switches to another font.

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.