114
\begin{tabular}{|l|l|l|}
\hline
Column A & Column B \\

\begin{math}
x & y \\
\end{math}

\end{tabular}

This won't let me compile and gives a lot of errors, how do I enable math mode in a table without using $ on everything?

6
  • 12
    If most terms in the table are going to be in math-mode, you could use an array environment (while in math-mode, obviously) instead of the tabular environment. If it's just the occasional column that's supposed to be in math mode, you could change its column type specifier from, say, c to >{$}c<{$}.
    – Mico
    May 5, 2013 at 12:11
  • 2
    To add to Mico's comment: ... and use \text{Column A} to switch to text mode in the column headings.
    – jub0bs
    May 5, 2013 at 12:12
  • @longtom $ ... $ is a short form for \begin{math} ... \end{math}, so using the latter is unnecessary verbose. May 5, 2013 at 12:18
  • 1
    Related question: tex.stackexchange.com/questions/106692/… May 5, 2013 at 12:19
  • 5
    You must load the array package to be able to use the advanced column specification thingy (e.g. >{$}c<{$}).
    – jub0bs
    May 5, 2013 at 12:59

3 Answers 3

121
\documentclass{article}

\usepackage{amstext} % for \text macro
\usepackage{array}   % for \newcolumntype macro
\newcolumntype{L}{>{$}l<{$}} % math-mode version of "l" column type


\begin{document}
\begin{tabular}{| L | L |}
\hline
\text{Column A} & \text{Column B} \\
x & y \\
\end{tabular}

\end{document}

enter image description here

4
  • 2
    Thanks, what do the less than and greater than symbols actually do?
    – Levi H
    May 5, 2013 at 13:08
  • 1
    They simply act as some kind of escape character there. They tell tabular that a special column specification follows.
    – jub0bs
    May 5, 2013 at 13:10
  • 5
    @STRAIGHTOUTTACOMPTON The Wikibooks page is basically indispensible for keeping track of how to use tables, at least for me.
    – Ryan Reich
    May 5, 2013 at 14:37
  • You don't have to use \text, you can use \multicolumn, too.
    – Skillmon
    Feb 20, 2019 at 18:04
39

If most of the table consists of math-mode material, it's preferable to use an array environment instead of a tabular environment. Any text-mode material in the table can be handled by encasing it in \text directives (requires the amsmath or the amstext package):

\documentclass{article}
\usepackage{amsmath} % for "\text" macro
\begin{document}
$\begin{array}{|c|c|}
\hline
\text{Column A} & \text{Column B} \\
x+y & x-y \\
\hline
\end{array}$
\end{document}
2
  • What if my table is all math, but I want to be able to caption and label it? Feb 10, 2018 at 10:44
  • @sarahjamal - Do note the use of \text directives to typeset the column headers. As you can probably guess, the argument of \text is typeset in text mode, not math mode. The material in the argument of \caption is typeset in text mode by default.
    – Mico
    Feb 10, 2018 at 10:54
27

If you use tabu, it automatically detects whether the table is in math mode, thus imitating this feature of array.

\documentclass{article}
\usepackage{amsmath,tabu}
\begin{document}
$\begin{tabu}{|l|l|}\hline
  \text{Column A} & \text{Column B} \\
  x & y
\end{tabu}$
\end{document}

enter image description here

3
  • 3
    One really nice feature of this approach (beyond not having to type the complicated array syntax above) is that it preserves syntax highlighting in my editor.
    – Reid
    Jan 23, 2014 at 0:08
  • This approach was very helpful to me, thanks. Dec 12, 2015 at 19:07
  • 1
    Note that tabu is not maintained anymore and likely to break at any point, cf. github.com/tabu-issues-for-future-maintainer/tabu
    – Clément
    Sep 21, 2021 at 4:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .