Handling "&" Characters in LaTeX Tables

I’m working with LaTeX tables for the first time using the LaTeX (Memoir Book) template and the “Table” section layout. I’ve got the table in the final ouput (using texmaker to compile) working fine but in LaTeX source I have to use the “&” character to define cells/columns in the table. At the same time, I have a replacement set for the entire document to replace “&” that I write to be “&” in the compile so that texmaker outputs the “&” in the final output.

Dilema. Can’t have it both ways at once.

So … the solution I’m assuming is my target is to tell the “Table” section layout to ignore the replacement list, or ignore the individual replacement of “&” to “&”. I can’t yet find a setting to instruction that layout to ignore the replacements.


1 Like

There are no exclusions or scopes for replacements. It would be very nice though, I agree. I’ve had to creatively work around that limitation a number of times—and most of the time that means pushing the replacement into a script, where I can employ additional logic or more complex conditional adjustments—and to be honest that’s fine for me most of the time. For example the keyboard shortcuts in the user manual are more complex than you might think, since depending upon the context of the modifier key I sometimes do not want a symbol or keycap font, but just want to say “press the Return key”.

But for this one, the simplest answer is to use something other than an ampersand in your tables and have the replacement that changes it back to an ampersand after the one that escapes the rest. The downside is that you can’t copy and paste the table into a native LaTeX editor though.

1 Like

Ah. Perfect. Got it. Wouldn’t have thought of that. I try to do all my LaTeX stuff in Scrivener, avoiding any LaTeX editor complications.

1 Like

Thanks for a great idea from @AmberV, I was able to get my journey into LaTeX tables from a Scrivener project working just fine.

In case others find this of interest for future use, here is what I do which works.

The issue is that LaTeX Tables use a & symbol to delineate table cells. I use & symbols in my writing and LaTeX considers & a special character and doesn’t print them on output. So I have a Scrivener replacement rule which converts all &'s into [backslash]&. The problem that creates is that it does that replacement on LaTeX table definitions preventing them from properly compiling into Tables.

So, here’s how I now handle with success.

One Time Steps for the Project (but I also put all this into my LaTeX template for future projects to avoid repeating these one-time steps):

  • In Scrivener replacements, after the & rule, I added a rule to convert all tick characters (`) into & symbols. I chose ticks as I do not use ticks in my writing. Any other non-used character can be used.

  • In my Preamble Settings where I put my own LaTeX packages, I added:
    % \usepackage{graphicx}
    % \usepackage{booktabs}

  • In the Table Section Layout, I made the changes:

    • in the Prefix, changed \begin{table}[H] – so that tables go “here”
    • in the Prefix and Suffix, moved the \caption line to after the lorem ipsum so that my table caption will be after the table (my convention for captions)
    • in the Suffix changed to \caption{$title} – to make the caption the name of the Scrivener Document.

For Each Table during the writing process:

  • I build the tables in Scrivener in their own document. I keep the tables simple with just rows and columns. I build in Scrivener as it’s easier. When the tables are basically done, I then make the LaTex version in a new document just under the Scrivener source.

  • I use https://www.tablesgenerator.com to generate the LaTeX table. I start by copy/paste from/to the Scrivener source into the gride on this web site. They provide full instructions. I use the “BookTabs” style choice. When completed, I “copy to clipboard” the generated code, then paste that into a new Scrivener Document just under the Scrivener Source. I name the document to what I want the table caption to be.

  • I use Scrivener Find/Replace to change all & to Ticks (``). I change the name of the Document to be what I want as a caption to the Table.

  • I remove the \begin{table} and \end{table} that the TablesGenerator provided since my Scrivener Table Section Layout includes that.

  • In the Compile List, I uncheck the document holding the Scrivener source for the table, and check-on the document holding the LaTeX code. I give that document a Section Type of “Table”.

  • Then compile to Plain Text and open in the “texmaker” app for to create the PDF.

If I did this a lot, I’d find ways to do a bit of automation, but that not a priority nor probably worth the time spent (for me).

Note: My own LaTeX template is based on the standard “General Non-Fiction (LaTeX)” template provided in Scrivener.


Hi rms,

I must be leading a charmed Scrivener life … I also use https://www.tablesgenerator.com to generate LaTeX tables A LOT!

Yet, for some reason, despite there being thousands of &'s scattered all through my Scrivener+LaTeX code in the dozens of Tables I’ve created … I’ve never had a problem with the & characters used to delineate the table columns.

For reference, I use the LaTeX (Memoir Book) Project Format, and I use TexShop to typeset my compiled .tex code.

So I have to wonder … why DOESN’T my Scrivener+LaTeX code bomb out all over the place ???

As an example, here are the first few dozen lines of code for one of the Tables I generated using the Tables Generator:

% Tables Generator
% Please add the following required packages to your document preamble:
% \usepackage{booktabs} and \usepackage{longtable}
% Note: It may be necessary to compile the document several times to get a multi-page table to line up properly
\caption{Big Oil,—,The oil market is bigger than all raw metal markets combined.\label{tab:BigOil-BiggerThanMetals }}
& \multicolumn{1}{c}{**\begin{tabular}[c]{@{}c@{}}Oil /\
Raw\* Material\* Extracted\end{tabular}} & {\begin{tabular}[c]{@{}c@{}}2015\* Worldwide\* Market Value \* (billion $)\end{tabular}} & {\begin{tabular}[c]{@{}c@{}}Percent of\* Oil\* Market\end{tabular}} & {\begin{tabular}[c]{@{}c@{}}Percent of\* Total Metals\* Market\end{tabular}}\altnotemark\* \midrule
{{\bfseries Table \thetable\ continued from previous page}} \*
& \multicolumn{1}{c}{
\begin{tabular}[c]{@{}c@{}}Oil /\* Raw\* Material\* Extracted\end{tabular}} & {\begin{tabular}[c]{@{}c@{}}2015\* Worldwide\* Market Value \* (billion $)\end{tabular}} & {\begin{tabular}[c]{@{}c@{}}Percent of\* Oil\* Market\end{tabular}} & {\begin{tabular}[c]{@{}c@{}}Percent of\* Total Metals\* Market\end{tabular}**} \* \midrule

The above code compiles and typesets just fine … without any errors!

This is NOT a complaint … just wondering why my typesetting app doesn’t scream at me with thousands of errors due to all the &'s (used in dozens of Tables) in my code??

Any clues would be greatly appreciated!

Thanks for reading,

The problem I had is sort of self-inflicted. I use & in normal writing, e.g. “Horace & Lamb” as a name of a firm or something. I want the & to appear in the LaTeX document. Unless I do something, it does not appear as it’s a special LaTeX code.

I use a compile replacement rule to:

Change all & to \&

To enable the LaTeX to compile. When it changes all the & in the LaTeX code to those two characters, LaTeX table does not appear.

So my “kludge” is to not do this replacement to the LaTeX code by not using & in my source LaTeX code, and use a replacement rule to convert my fake code to & as the last replacement step in a compile. Using & in the LaTeX code exported from the Scrivener compile, just works.

Your code works as you have used & characters. If they were all replaced by a backslash in front of the &,

\& instead of &

I doubt the code would work. I suppose depends on the LaTeX compiler. Above my pay grade.

1 Like

Hi rms,

Thanks for the explanation … I guess I learned to place a \ in from of those pesky LaTeX characters, including &, before I learned (or have I?) to use a “compile replacement rule”.

It can get a bit annoying when I forget to place the \ before all the &'s in the text … I still get caught forgetting about the ampersands more than care to admit …


Hi AmberV,

Is there a best place to start learning about how to use a “compile replacement rule”.


i am pretty sure there is a section in the Manual which describes it.

1 Like

Hi rms (and AmberV),

Oh … thanks … I’d forgotten about that … thought it was a LaTeX feature, not Scrivener … used the Replacements screenimage about 2 years ago, then forgot about it … the problem with early Alzheimers is forgetting what you did years ago.

Apologies for the question … thanks for your response.