\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?
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It only takes a minute to sign up.
Sign up to join this community\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}
tabular
that a special column specification follows.
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}
\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.
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}
array
environment (while in math-mode, obviously) instead of thetabular
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<{$}
.\text{Column A}
to switch to text mode in the column headings.$ ... $
is a short form for\begin{math} ... \end{math}
, so using the latter is unnecessary verbose.array
package to be able to use the advanced column specification thingy (e.g.>{$}c<{$}
).