[Tex/LaTex] Escaping ampersand inside of a variable within \multicolumn text

ampersandmulticolumnPHPtables

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] }
\arrayrulecolor{medium-gray}
\multicolumn{5}{ l }{\large{\textbf{%=expense=%}}} & \\
\textbf{Date} & \textbf{Employee} & \textbf{Quantity} & \textbf{Cost} &
    \textbf{Total} & \textbf{Notes} \tabcr
\endhead
%=date=% & %=employee=% & %=qty=% & %=cost=% & %=total=% & %=note=% \tabcr
\end{longtabu}

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?

Best Answer

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.