CSV to LaTeX Table Converter

A CSV to LaTeX converter turns a spreadsheet into a table you can paste into a paper or a thesis. This one escapes all ten LaTeX special characters in the right order, offers booktabs, plain tabular or longtable for data that runs past a page, right aligns your numeric columns, and can hand you a complete .tex file that compiles as it stands. Nothing is uploaded.

Need to round, rename or reorder columns first? Open the app

Ten characters, and the order you replace them in

LaTeX has ten characters that mean something other than themselves. Seven of them take a backslash:

&  %  $  #  _  {  }   →   \&  \%  \$  \#  \_  \{  \}

Two of them do not. A bare ~ is a non-breaking space, so it prints as a gap rather than a tilde and needs \textasciitilde{}. A bare ^ starts a superscript, so a^b silently raises the b and needs \textasciicircum{}.

The tenth is the backslash itself, and it is the one that catches naive implementations. You cannot escape it with a backslash, because \\ is a line break and inside a table it ends the row. It needs \textbackslash{}. And crucially, it has to be replaced first, through a placeholder, because the other nine replacements all introduce backslashes of their own. Replace them in the obvious order and a cell containing 100% comes out as 100\textbackslash{}%, which is worse than doing nothing.

A supplier column with Fenwick & Sons in it, a discount column of 10%, a price in dollars and a Windows path all appear in ordinary business data, and any one of them turns into a compiler error forty lines from where the problem actually is. The error LaTeX gives you for an unescaped ampersand is Misplaced alignment tab character &, which is accurate and tells you nothing about which cell.

A worked example

Four products, with every escaping trap in them:

sku,name,price,discount,supplier
00412,"Bracket, aluminium 40mm",4.25,10%,Nordwerk GmbH
00907,"Bracket, aluminium 90mm",6.80,10%,Nordwerk GmbH
01730,Bronze bushing 17mm,2.15,,Fenwick & Sons
00088,"Cable clip, nylon",0.09,5%,Ashby Supplies

With booktabs and a caption, out comes:

\begin{table}[htbp]
  \centering
  \caption{Component prices}
  \label{tab:prices}
  \begin{tabular}{lllll}
    \toprule
    \textbf{sku} & \textbf{name} & \textbf{price} & \textbf{discount} & \textbf{supplier} \\
    \midrule
    00412 & Bracket, aluminium 40mm & 4.25 & 10\% & Nordwerk GmbH \\
    00907 & Bracket, aluminium 90mm & 6.80 & 10\% & Nordwerk GmbH \\
    01730 & Bronze bushing 17mm & 2.15 &  & Fenwick \& Sons \\
    00088 & Cable clip, nylon & 0.09 & 5\% & Ashby Supplies \\
    \bottomrule
  \end{tabular}
\end{table}

The percent signs and the ampersand are escaped. There are no vertical rules and no \hline, which is what booktabs is for. And the price column is left aligned rather than right, which looks like a bug and is not: 6.80 written as a number is 6.8, so that column is text, and text is left aligned. A column of prices that all had a non-zero last digit would be typed numeric and right aligned. Types are decided once for the whole column, never cell by cell, so you never get half a column aligned one way.

Booktabs, tabular, and the argument between them

The booktabs package documentation is unusually opinionated for a LaTeX manual, and its two main rules are: never use vertical rules, and never use double horizontal rules. What it recommends instead is three rules total, a \toprule, a \midrule under the header, and a \bottomrule, with white space doing the work that lines were doing badly.

A table with \hline after every row and pipes between every column is precisely the thing that argument is against. It is also what most generated LaTeX looks like, and what a lot of departmental templates still specify. So both are here. Booktabs is the default because it is what a table in a published paper looks like; the hline style is one dropdown away when a supervisor or a journal template asks for it.

If you pick booktabs and are not writing a standalone file, the output tells you to add \usepackage{booktabs} to your preamble. Forgetting that is the second most common reason this kind of table fails to compile, after unescaped ampersands.

Longtable, for data that does not fit on a page

A tabular inside a table float is one indivisible box. If it is taller than the page, LaTeX cannot break it: it will float the whole thing to the end of the chapter, or let it run off the bottom margin, depending on the placement you gave it. Neither is what you wanted, and the fix is not a placement specifier, it is a different environment.

longtable breaks across pages properly. Getting it right by hand is fiddly, because it needs four separate header and footer blocks, and this converter writes all of them:

\begin{longtable}{lrr}
  \caption{Long results}\label{tab:long} \\
  \toprule
  \textbf{id} & \textbf{n} & \textbf{value} \\
  \midrule
  \endfirsthead          % header for page 1
  \toprule
  \textbf{id} & \textbf{n} & \textbf{value} \\
  \midrule
  \endhead               % header repeated on every later page
  \midrule
  \multicolumn{3}{r}{\footnotesize\itshape Continued on the next page} \\
  \endfoot               % footer on every page but the last
  \bottomrule
  \endlastfoot           % footer on the final page
  ...rows...
\end{longtable}

A longtable is not a float, so it appears exactly where you put it in the source, and it does not take a [htbp] placement. Note also that it needs two compilation passes to settle its column widths; if the first run looks misaligned, run it again before you start debugging.

Wide tables, labels and line breaks

More than about twelve columns will not fit the text width of a portrait page, and LaTeX does not wrap a tabular, it lets it overflow into the margin with an overfull hbox warning you will probably not read. When the column count crosses that line you get a warning here suggesting landscape via the pdflscape package, a smaller font size, or tabularx with an X column that absorbs the slack. A standalone file gets landscape geometry set for you.

A label with no prefix gets a nudge. The convention is tab: for a table, fig: for a figure, eq: for an equation, and the cleveref package relies on it to write "Table 3" rather than just "3". A label of results works but tells \cref nothing.

A line break inside a cell becomes a space, and the count is reported. There is no way to keep it without changing the column type: a \\ inside a cell ends the row, and a real wrapped paragraph needs a p{width} column, which changes the geometry of the whole table and needs a width you have to choose. Flattening and saying so leaves that decision with you.

Questions

Which characters does LaTeX need escaped, and are they all handled?

Ten. Seven take a backslash: ampersand, percent, dollar, hash, underscore, and the two braces. Two cannot, because a bare tilde is a non-breaking space and a bare caret starts a superscript, so they need textasciitilde and textasciicircum. And the backslash itself needs textbackslash, since a doubled backslash is a line break. All ten are handled, and the backslash is replaced first through a placeholder so the escapes introduced by the other nine do not get escaped in turn.

Should I use booktabs or tabular with hlines?

Booktabs unless a house style forbids it. The booktabs manual opens by telling you never to use vertical rules and never to use double horizontal rules, and a table with an hline after every row is exactly what it is arguing against. Booktabs gives you a thick rule at the top, a thin one under the header, and a thick one at the bottom, which is what a table in a journal looks like. Tabular with hlines is here because some templates and supervisors still ask for it.

What do I do when the table runs past one page?

Use longtable. A tabular inside a table float cannot break across pages: LaTeX will either push the whole thing to the end of the document or let it run off the bottom. Longtable is a different environment that breaks properly, repeats the header on every continuation page, and prints a continued line at the foot. This converter writes the endfirsthead and endhead blocks that make that work, which is the fiddly part.

Are numbers aligned?

Columns typed as numeric get an r in the column specification, so the digits line up and a column of figures can be read down. Everything else gets an l. Types are decided once per column by reading the whole column, so a column mixing 9.99 and 12.50 stays text and left aligned rather than half one and half the other. You can force all left, centre or right if the automatic choice is wrong.

Can I get a complete file I can compile straight away?

Yes. Switch the output to a full .tex and you get a document class, the inputenc and fontenc packages, geometry, and whichever of booktabs and longtable your table needs. It compiles with pdflatex as it stands, which is useful for checking the table before pasting it into a real document. A table more than about twelve columns wide also gets landscape geometry, with a note saying why.

Why is my caption above the table?

Convention, and the booktabs manual's recommendation. A table caption goes above the table and a figure caption goes below it, which is what most journal styles enforce. If yours wants it below, move the caption line after the tabular environment in the output; nothing else needs to change.

What happens to a line break inside a cell?

It becomes a space, and the count is reported. A tabular column has no notion of a wrapped paragraph: a double backslash inside a cell ends the row, and making it work properly means switching that column to a paragraph type such as p or a tabularx X column, which changes the whole table's geometry. Turning the break into a space is the honest, non-destructive choice, and knowing it happened lets you decide whether that column needs a different type.

Convert your CSV to a LaTeX table

No sign-up, no upload, no row cap. Booktabs or longtable, every special character escaped, numbers aligned.

Back to the converter