Graphics, Figures & TablesUsing \text in math column in tabular

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
Singularity
Posts: 156
Joined: Sat Jan 22, 2011 9:55 pm

Using \text in math column in tabular

Post by Singularity »

I have defined special math columns for my tabular, which I got straight out of the array documentation. But I want the column headings to be text. Why can't I use text in my column content?

Code: Select all

\documentclass{article}

\usepackage{array}					% Provides for a more flexible array and tabular environment, like the column defs below
\usepackage{booktabs}				% For fancy stuff in arrays and tables
\usepackage{multicol,multirow}
\newcolumntype{L}{>{\begin{math}}l<{\end{math}}}%
\newcolumntype{C}{>{\begin{math}}c<{\end{math}}}%
\newcolumntype{R}{>{\begin{math}}r<{\end{math}}}%
\newcolumntype{B}{>{\textbf{}l<{}}}%

\begin{document}
\title{Conic Sections}

\begin{tabular}{|l|L|L|}
	\toprule
	\multicolumn{3}{c}{\large\textbf{Parabola}\normalsize} \\
	\toprule
		& \text{Vertical Axis} & \text{Horizontal Axis} \\
	\midrule
	Equation & \left(x-h\right)^2 = 4p \left(y-k\right) & \left(y-k\right)^2 = 4p \left(x-h\right) \\
	\midrule
	Axis of symmetry & x=h & y=k \\
	\midrule
	Vertex & \left(h,k\right) & \left(h,k\right) \\
	\midrule
	Focus & \left(h,k+p\right) & \left(h+p,k\right) \\
	\midrule
	Directrix & y=k-p & x=h-p \\
	\midrule
	Direction of opening & p>0 \Rightarrow up &  p>0 \Rightarrow right \\
		& p<0 \Rightarrow down	&  p<0 \Rightarrow left 	\\
	\bottomrule
	\multicolumn{3}{l}{Note: $d_1=d_2$, dist. to directrix=dist. to focus} \\
	\bottomrule
\end{tabular}

\end{document}

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Re: Using \text in math column in tabular

Post by Johannes_B »

Hi, can't test right now. Since your columns are in math mode, you have to escape to text mode. I remember an example in the doc using a dollar sign to open and end math mode in the column declaratation. When using a dollar sign inside the tabular it will end the formerly opened column and the next dollar sign will open the math environment which is closed by the dollar sign in the declaration. I hope you understand what i was just writing, but i fear not. Maybe take a pencil and write the steps down, it will be obvious ;-)

Problem could have another reason though, as i said i can't test. But i'll have a look at this later.
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Using \text in math column in tabular

Post by Johannes_B »

Hi, the problem was much much simpler. The \text command is defined with package amsmath. Since there are some little bugs in amsmath, package mathtools comes to help. Just including this package in your document, and you are fine :-)

Code: Select all

\documentclass{article}

\usepackage{array}                                      % Provides for a more flexible array and tabular environment, like the column defs below
\usepackage{mathtools}                                      % Provides for a more flexible array and tabular environment, like the column defs below
\usepackage{booktabs}                           % For fancy stuff in arrays and tables
\usepackage{multicol,multirow}
\newcolumntype{L}{>{\begin{math}}l<{\end{math}}}%
\newcolumntype{C}{>{\begin{math}}c<{\end{math}}}%
\newcolumntype{R}{>{\begin{math}}r<{\end{math}}}%
\newcolumntype{B}{>{\textbf{}l<{}}}%

\begin{document}
\title{Conic Sections}

\begin{tabular}{|l|L|L|}
	\toprule
	\multicolumn{3}{c}{\large \textbf{Parabola}\normalsize} \\
	\multicolumn{3}{c}{\large \textbf{Parabola}} \\
	\toprule
	& \text{Vertical Axis} & \text{Horizontal Axis} \\
	\midrule
	Equation & \left(x-h\right)^2 = 4p \left(y-k\right) & \left(y-k\right)^2 = 4p \left(x-h\right) \\
	\midrule
	Axis of symmetry & x=h & y=k \\
	\midrule
	Vertex & \left(h,k\right) & \left(h,k\right) \\
	\midrule
	Focus & \left(h,k+p\right) & \left(h+p,k\right) \\
	\midrule
	Directrix & y=k-p & x=h-p \\
	\midrule
	Direction of opening & p>0 \Rightarrow up &  p>0 \Rightarrow right \\
	& p<0 \Rightarrow down  &  p<0 \Rightarrow left         \\
	\bottomrule
	\multicolumn{3}{l}{Note: $d_1=d_2$, dist. to directrix=dist. to focus} \\
	\bottomrule
\end{tabular}

\end{document}
Little hint on the multicolumn: multicolumns are completely independant of the surroundings, no former math switch will be known there. By the way, all those cells in a tabular are independent, so there is no real use in switching back to \normalsize. Please keep in mind, that changes of the font size are tricky. They should always be done the following way

Code: Select all

{\Large this is some large text\par}
That means putting the switch and the text inside a group to keep the change of size local. But the \par is really important since it takes care of the interline spacing.


\textbf works in a very similar manner. Internally, \textbf{this is fat text} is expanded to

Code: Select all

{\bfseries this is fat text}
. To be honest, the internals are a little bit more complicated ;-)
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
Post Reply