1

I'm trying to place some equations inside a table using the following code:

\documentclass[a4paper]{article}

\begin{document}

\begin{tabular}{ |p{0.5cm}||p{3.5cm}|p{3.8cm}|p{3.1cm}|p{3.6cm}| }
    \hline
    $Y_{ \textcolor{red}{l}, \textcolor{blue}{m} }$& \textcolor{blue}{0}& \textcolor{blue}{1}& \textcolor{blue}{2}& \textcolor{blue}{3}\\
    \hline
    \textcolor{red}{0}  & $\frac{1}{2} \sqrt{ \frac{1}{\pi} }$  &- &- & - \\
    \hline
    \textcolor{red}{1} & $\frac{1}{2} \sqrt{\frac{3}{\pi}} \cos\theta$ & $-\frac{1}{2} \sqrt{\frac{3}{2\pi}} e^{i\phi}\sin\theta$  &- &- \\
    \hline
    \textcolor{red}{2} & $\frac{1}{4} \sqrt{\frac{5}{\pi}} (3\cos^{2}\theta - 1)$ & $-\frac{1}{2} \sqrt{\frac{15}{2\pi}}e^{i\phi}\sin\theta \cos\theta$ &$\frac{1}{4}\sqrt{\frac{15}{2\pi}}e^{2i\phi}\sin^{2}\theta$   &-\\
    \hline
    \textcolor{red}{3} & $\frac{1}{4}\sqrt{\frac{7}{\pi}} (5\cos^{4}\theta - 3\cos\theta)$ & $-\frac{1}{8}\sqrt{ \frac{21}{\pi} } e^{2i\phi}\sin\theta(5\cos^{2}\theta - 1)$ & $\frac{1}{4}\sqrt{ \frac{105}{2\pi} } e^{2i\phi} \sin^{2}\theta \cos\theta$  & $-\frac{1}{8}\sqrt{ \frac{35}{\pi} }e^{3i\phi} \sin^{3}\theta$  \\
    \hline
\end{tabular} 

\end{document}

What is get is:

enter image description here

What could be done to make the table:

  1. Fit in the page?
  2. Look more centered?

Mico
  • 506,678
Ziezi
  • 455

2 Answers2

2

A solution with tabularx, nccmath (for its medsize environment), hhline for nicely intersecting vertical and horizontal rules and cellspace for a minimal padding at the top and bottom of cells in columns withspecifier prefixed with the letter S:

\documentclass[a4paper]{article}
\usepackage{geometry}
\usepackage{nccmath}
\usepackage{xcolor}
\usepackage{tabularx, hhline}
\newcolumntype{Y}{>{\centering\arraybackslash$\medsize}S{X}<{\endmedsize$}}
\newcolumntype{Z}{>{\hsize =0.8\hsize\centering\arraybackslash$\medsize}S{X}<{\endmedsize$}}
\newcolumntype{W}{>{\hsize =1.2\hsize\centering\arraybackslash$\medsize}S{X}<{\endmedsize$}}
\usepackage{cellspace}
\setlength{\cellspacetoplimit}{4pt}
\setlength{\cellspacebottomlimit}{4pt}
\addparagraphcolumntypes{X, Y}

\begin{document}

{\setlength{\tabcolsep}{4pt}%
\centering\small\begin{tabularx}{\linewidth}{|l||S{Y}|W|S{Y}|Z|}
 \hhline{|-||----|}
 $Y_{ \textcolor{red}{l}, \textcolor{blue}{m} }$& \textcolor{blue}{0}& \textcolor{blue}{1}& \textcolor{blue}{2}& \textcolor{blue}{3}\\
    \hhline{|-||----|}
    \textcolor{red}{0} & \mfrac{1}{2} \sqrt{ \mfrac{1}{\pi} } &- &- & - \\
    \hhline{|-||----|}
    \textcolor{red}{1} & \mfrac{1}{2} \sqrt{\mfrac{3}{\pi}} \cos\theta & -\mfrac{1}{2} \sqrt{\mfrac{3}{2\pi}} e^{i\phi}\sin\theta &- &- \\
    \hhline{|-||----|}
    \textcolor{red}{2} & \mfrac{1}{4} \sqrt{\mfrac{5}{\pi}} (3\cos^{2}\theta - 1) & -\mfrac{1}{2} \sqrt{\mfrac{15}{2\pi}}e^{i\phi}\sin\theta \cos\theta &\mfrac{1}{4}\sqrt{\mfrac{15}{2\pi}}e^{2i\phi}\sin^{2}\theta &-\\
    \hhline{|-||----|}
    \textcolor{red}{3} & \mfrac{1}{4}\sqrt{\mfrac{7}{\pi}} (5\cos^{4}\theta - 3\cos\theta) & -\mfrac{1}{8}\sqrt{ \mfrac{21}{\pi} } e^{2i\phi}\sin\theta(5\cos^{2}\theta - 1) & \mfrac{1}{4}\sqrt{ \mfrac{105}{2\pi} } e^{2i\phi} \sin^{2}\theta \cos\theta & -\mfrac{1}{8}\sqrt{ \mfrac{35}{\pi} }e^{3i\phi} \sin^{3}\theta \\
    \hhline{|-||----|}
\end{tabularx}
}

\end{document} 

enter image description here

Bernard
  • 271,350
1

Assuming your document uses standard margin widths (ca. 2.5cm), all you need to do is employ an array environment -- the contents of an array are automatically in math mode, eliminating the need for lots and lots of $ symbols to start and end math mode -- and reduce the value of \arraycolsep slightly, from the default value of 5pt down to 4pt; the parameter \arraycolsep governs the amount of intercolumn whitespace.

I would also give the table a more open "look", by (a) getting rid of all vertical lines and three of the interior horizontal lines and (b) employing the rule-drawing macros of the booktabs package. Finally, I'd center the cell contents rather than aligning them on the left-hand cell borders.

enter image description here

\documentclass[a4paper]{article}
\usepackage[margin=2.5cm]{geometry} % set the page parameters suitably
\usepackage{xcolor} % for \textcolor macro
\usepackage{booktabs} % for \toprule, \midrule, \bottomrule, and \addlinespace macros
\begin{document}

\begin{center}
\setlength\arraycolsep{4pt} % default: 5pt
$\begin{array}{@{} *{5}{c} @{}}
\toprule
Y_{ \textcolor{red}{l}, \textcolor{blue}{m} } &  
  \textcolor{blue}{0} & \textcolor{blue}{1} & 
  \textcolor{blue}{2} & \textcolor{blue}{3} \\
\midrule
\textcolor{red}{0}  & 
\frac{1}{2} \sqrt{ \frac{1}{\pi} }  
&- &- & - \\
\addlinespace
\textcolor{red}{1} & 
 \frac{1}{2} \sqrt{\frac{3}{\pi}} \cos\theta & 
-\frac{1}{2} \sqrt{\frac{3}{2\pi}} e^{i\phi}\sin\theta  
&- &- \\
\addlinespace
\textcolor{red}{2} & 
 \frac{1}{4} \sqrt{\frac{5}{\pi}} (3\cos^{2}\theta - 1) & 
-\frac{1}{2} \sqrt{\frac{15}{2\pi}}e^{i\phi}\sin\theta \cos\theta &
 \frac{1}{4}\sqrt{\frac{15}{2\pi}}e^{2i\phi}\sin^{2}\theta   
&-\\
\addlinespace
\textcolor{red}{3} & 
 \frac{1}{4}\sqrt{\frac{7}{\pi}} (5\cos^{4}\theta - 3\cos\theta) & 
-\frac{1}{8}\sqrt{ \frac{21}{\pi} } e^{2i\phi}\sin\theta(5\cos^{2}\theta - 1) & 
 \frac{1}{4}\sqrt{ \frac{105}{2\pi} } e^{2i\phi} \sin^{2}\theta \cos\theta  & 
-\frac{1}{8}\sqrt{ \frac{35}{\pi} }e^{3i\phi} \sin^{3}\theta  \\
\bottomrule
\end{array}$ 
\end{center}
\end{document}
Mico
  • 506,678