Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
\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?

share|improve this question
2  
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 '13 at 12:11
1  
To add to Mico's comment: ... and use \text{Column A} to switch to text mode in the column headings. –  Jubobs May 5 '13 at 12:12
    
@longtom $ ... $ is a short form for \begin{math} ... \end{math}, so using the latter is unnecessary verbose. –  Torbjørn T. May 5 '13 at 12:18
1  
Related question: tex.stackexchange.com/questions/106692/… –  Torbjørn T. May 5 '13 at 12:19
1  
You must load the array package to be able to use the advanced column specification thingy (e.g. >{$}c<{$}). –  Jubobs May 5 '13 at 12:59

2 Answers 2

up vote 18 down vote accepted
\documentclass{article}

\usepackage{amstext}    % defines the \text command, needed here
\usepackage{array}      % for advanced column specification: use >{\command} for
                        % commands executed right before each column element
                        % and <{\command} for commands to be executed right after
                        % each column element

\begin{document}
\begin{tabular}{| >{$}l<{$} | >{$}l<{$} | >{$}l<{$} |}
\hline
\text{Column A} & \text{Column B} \\
x & y \\
\end{tabular}

\end{document}

enter image description here

share|improve this answer
1  
(\text is defined in amstext.sty which is loaded by amsmath.sty.) –  Qrrbrbirlbel May 5 '13 at 13:07
    
@Qrrbrbirlbel Ok. I didn't know that, but I guess the OP will need amsmath anyway :) –  Jubobs May 5 '13 at 13:08
1  
Thanks, what do the less than and greater than symbols actually do? –  STRAIGHT OUTTA COMPTON May 5 '13 at 13:08
1  
They simply act as some kind of escape character there. They tell tabular that a special column specification follows. –  Jubobs May 5 '13 at 13:10
2  
@STRAIGHTOUTTACOMPTON The Wikibooks page is basically indispensible for keeping track of how to use tables, at least for me. –  Ryan Reich May 5 '13 at 14:37

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

share|improve this answer
    
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 at 0:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.