2

I want to create a table with text and math symbols in its cells. The problem is that I want in each cell the context to be vertically aligned to center.

Below you can see what I have done so far

\documentclass[a4paper,12pt]{report}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{amsfonts}

\usepackage[table]{xcolor}

\begin{document}

   \newcolumntype{K}{>{\centering}m{4.5cm}}
   \newcolumntype{L}{>{\centering}m{3.5cm}}
   \newcolumntype{M}{@{}m{0pt}@{}}

   \begin{tabular}{|c|K|c|c|L|M|}
      \multicolumn{2}{c}{\cellcolor{cyan}\textbf{Column A}} &  &  \multicolumn{2}{c}{\cellcolor{cyan}\textbf{Column B}}\\[5pt]
      a.  & $5x^3(x - 1)^3$, $2x(x - 1)^4$   &  & 1. & $-10x^4(x - 1)^3$ &\\[20pt]
      \hline        
      b.  & $5x(x - 1)^2$, $2x^3(x - 1)$     &  & 2. & $10x^2(x - 1)$ &\\[20pt]
      \hline            
      c.  & $5x^4(x^2 - 1)$, $2x^2(x - 1)^2$  &  & 3. & $10x^2(x - 1)^4$ &\\[20pt]
      \hline            
      d.  & $5x^2(x - 1)^2$, $2x^4(1 - x)^3$ &  & 4. & $10x^3(x - 1)^2$ &\\[20pt]
      \hline        
      c.  & $\dfrac{x^2}{x + 1}$                       &  & 5. & $10x^4(x - 1)^2(x + 1)$ &\\
      \hline
   \end{tabular}

\end{document}

As you can see the text in the first row isn't vertically aligned. Any suggestions?

3 Answers 3

1

You can fake it by adding an extra line at the top, in the form of

\multicolumn{2}{c|}{\cellcolor{cyan}} &  &  \multicolumn{2}{c}{\cellcolor{cyan}}\\[-5pt]

Here is the MWE. I also changed the leading multi-column format to c| to make the vertical black lines symmetrical abutting the cyan border.

\documentclass[a4paper,12pt]{report}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{amsfonts}

\usepackage[table]{xcolor}

\begin{document}

   \newcolumntype{K}{>{\centering}m{4.5cm}}
   \newcolumntype{L}{>{\centering}m{3.5cm}}
   \newcolumntype{M}{@{}m{0pt}@{}}

   \begin{tabular}{|c|K|c|c|L|M|}
      \multicolumn{2}{c|}{\cellcolor{cyan}} &  &  \multicolumn{2}{c}{\cellcolor{cyan}}\\[-5pt]
      \multicolumn{2}{c|}{\cellcolor{cyan}\textbf{Column A}} &  &  \multicolumn{2}{c}{\cellcolor{cyan}\textbf{Column B}}\\[5pt]
      a.  & $5x^3(x - 1)^3$, $2x(x - 1)^4$   &  & 1. & $-10x^4(x - 1)^3$ &\\[20pt]
      \hline        
      b.  & $5x(x - 1)^2$, $2x^3(x - 1)$     &  & 2. & $10x^2(x - 1)$ &\\[20pt]
      \hline            
      c.  & $5x^4(x^2 - 1)$, $2x^2(x - 1)^2$  &  & 3. & $10x^2(x - 1)^4$ &\\[20pt]
      \hline            
      d.  & $5x^2(x - 1)^2$, $2x^4(1 - x)^3$ &  & 4. & $10x^3(x - 1)^2$ &\\[20pt]
      \hline        
      c.  & $\dfrac{x^2}{x + 1}$                       &  & 5. & $10x^4(x - 1)^2(x + 1)$ &\\
      \hline
   \end{tabular}

\end{document}

enter image description here

If you wanted that last row fixed up too, use the stackengine package and employ an \addstackgap[]{} with the $\dfrac{x^2}{x + 1}$entry:

      c.  & \addstackgap{$\dfrac{x^2}{x + 1}$}  &  & 5. & $10x^4(x - 1)^2(x + 1)$ &\\

to achieve

enter image description here

1

m columns lose baseline alignment so I wouldn't use them for short one-line examples. here I've simplified the table markup getting rid of the "extra" columns, and just opening up the table a bit. I left in the full width \hline as you have them, but I'd be tempted to make them not cross the middle, or just not have the lines at all.

enter image description here

\documentclass[a4paper,12pt]{report}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{amsfonts}

\usepackage[table]{xcolor}

\begin{document}

   \newcolumntype{K}{>{\centering\arraybackslash}p{4.5cm}}
   \newcolumntype{L}{>{\centering\arraybackslash}p{3.5cm}}

   {\renewcommand\arraystretch{2.5}%
   \begin{tabular}{|c|K|@{\hspace{1cm}}|c|L|}
      \hline
      \multicolumn{2}{|c|@{\hspace{1cm}}|}{\cellcolor{cyan}\textbf{Column A}} & 
    \multicolumn{2}{c|}{\cellcolor{cyan}\textbf{Column B}}\\
      \hline
      a.  & $5x^3(x - 1)^3$, $2x(x - 1)^4$   &  1. & $-10x^4(x - 1)^3$ \\
      \hline
      b.  & $5x(x - 1)^2$, $2x^3(x - 1)$     &  2. & $10x^2(x - 1)$ \\
      \cline{1-2}\cline{3-4}          
      c.  & $5x^4(x^2 - 1)$, $2x^2(x - 1)^2$  &  3. & $10x^2(x - 1)^4$ \\
      \hline            
      d.  & $5x^2(x - 1)^2$, $2x^4(1 - x)^3$ &  4. & $10x^3(x - 1)^2$ \\
      \hline        
      c.  & $\dfrac{x^2}{x + 1}$             & 5. & $10x^4(x - 1)^2(x + 1)$ \\
      \hline
   \end{tabular}}

\end{document}
1

The simplest way to have vertically centred cells is with the cellspacepackage: it defines (globally) minimal vertical spacing above and below cell contents in columnx with specifier prefixed with the letter S (or C if you load siunitx).

The following code uses only c or Sccolumns. Additionally, I used \hhlines, and added a line under the coloured column heads, as I found it neater:

\documentclass[a4paper,12pt]{report}

\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{array, cellspace, hhline}
\setlength\cellspacetoplimit{15pt}
\setlength\cellspacebottomlimit{15pt}
\usepackage[table]{xcolor}

\begin{document}

   \begin{tabular}{|*{2}{Sc|}p{2em}*{2}{|Sc}|}
      \multicolumn{2}{Sc}{\cellcolor{cyan}\textbf{Column A}} & & \multicolumn{2}{>{\columncolor{cyan}[6.4pt][6pt]}c}{\cellcolor{cyan}\textbf{Column B}}\\
      \hhline{--~|--}
      a. & $5x^3(x - 1)^3$, $2x(x - 1)^4$ & & 1. & $-10x^4(x - 1)^3$ \\
      \hhline{--~|--}
      b. & $5x(x - 1)^2$, $2x^3(x - 1)$ & & 2. & $10x^2(x - 1)$ \\
      \hhline{--~|--}
      c. & $5x^4(x^2 - 1)$, $2x^2(x - 1)^2$ & & 3. & $10x^2(x - 1)^4$ \\
      \hhline{--~|--}
      d. & $5x^2(x - 1)^2$, $2x^4(1 - x)^3$ & & 4. & $10x^3(x - 1)^2$ \\
      \hhline{--~|--}
      c. & $\smash{\dfrac{x^2}{x + 1}}$ & & 5. & $10x^4(x - 1)^2(x + 1)$ \\
      \hhline{--~|--}
   \end{tabular}

\end{document} 

enter image description here

You must log in to answer this question.

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