6

I want to make a table with vertically centered columns in a \tabularx environment. How can I code the table to have all the column types (l, c, X) vertically centered?

Here is my code:

\begin{table}[h]
\centering
\caption[Text]{Text.}
\label{tab:5599}

\begin{tabularx}{\textwidth}{|l|c|X|}
\hline

\textit{} & \textit{A} & \textit{B} \\
\thickhline

A & B & C
\hline

\end{tabularx}
\end{table}

EDIT:

I am still facing the problem that my text is not vertically centered:

\begin{table}[h]
\centering
\caption[Material for SDS-PAGE]{Buffer compositions for SDS-PAGE.}
\label{tab:8899}
\begin{tabularx}{\textwidth}{Xcccc} % way of defining a fix columns width
\toprule[1.5pt]
\textit{} & \textit{Resolving gel} & \textit{Stacking gel} & \textit{Laemmli sample buffer} & \textit{Running buffer} \\ 
\midrule[1.5pt]
Acrylamide & 10 \% & 5 \% & - & - \\
\midrule
Tris-HCl (1.5 M, pH 8.8) & 25 \% & - & - & - \\
\midrule
Tris-HCl (1.0 M, pH 6.8) & - & 12.5 \% & 6.25 \% & - \\
\midrule
SDS & 0.1 \% & 0.1 \% & - & - \\
\midrule
Ammonium persulfate & 0.1 \% & 0.1 \% & - & - \\
\midrule
TEMED & 0.04 \% & 0.1 \% & - & - \\
\midrule
2-Mercaptoethanol & - & - & 0.1 \% & - \\
\midrule
Bromophenol blue & - & - & 0.0005 \% & - \\
\midrule
Glycerol & - & - & 10 \% & - \\
\midrule
TRIS & - & - & - & 0.25 M \\
\midrule
Glycine & - & - & - & 1.92 M \\
\midrule
pH & - & - & - & 8.3 \\
\midrule
H\textsubscript{2}O & Solvent & Solvent & Solvent & Solvent \\
\bottomrule[1.5pt]
\end{tabularx}
\end{table}

Here is the output:

enter image description here

  • Please make your code compilable. In my eyes, they are vertically centered. Could you show a screenshot? I do not know \thickline and there seems to be a \\ missing after C. – LaRiFaRi Dec 8 '16 at 12:55
18

It seems that addition of

\renewcommand\tabularxcolumn[1]{m{#1}}% for vertical centering text in X column

in your preamble is what you looking for:

enter image description here

\documentclass{article}
\usepackage{geometry}
\usepackage{booktabs, tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}% for vertical centering text in X column

\begin{document}

    \begin{table}[h]
    \centering
\caption[Material for SDS-PAGE]{Buffer compositions for SDS-PAGE.}
    \label{tab:8899}
\begin{tabularx}{\textwidth}{>{\raggedright}Xcccc} % way of defining a fix columns width
    \toprule[1.5pt]
    &   \textit{Resolving gel} 
        &   \textit{Stacking gel} 
            &   \textit{Laemmli sample buffer} 
                &   \textit{Running buffer}     \\
    \midrule[1.5pt]
Acrylamide                  & 10 \%     & 5 \%      &  --       &  --   \\
    \midrule
Tris-HCl (1.5 M, pH 8.8)    & 25 \%     &  --       &  --       &  --   \\
    \midrule
Tris-HCl (1.0 M, pH 6.8)    &  --       & 12.5 \%   & 6.25 \%   &  --   \\
    \midrule
SDS                         & 0.1 \%    & 0.1 \%    &  --       &  --   \\
    \midrule
Ammonium persulfate         & 0.1 \%    & 0.1 \%    &  --       &  --   \\
    \midrule
TEMED                       & 0.04 \%   & 0.1 \%    &  --       &  --   \\
    \midrule
2-Mercaptoethanol           &  --       &  --       & 0.1 \%    &  --   \\
    \midrule
Bromophenol blue            &  --       &  --       & 0.0005 \% &  --   \\
    \midrule
Glycerol                    &  --       &  --       & 10 \%     &  --   \\
    \midrule
TRIS                        &  --       &  --       &  --       & 0.25 M\\
    \midrule
Glycine                     &  --       &  --       &  --       & 1.92 M\\
    \midrule
pH                          &  --       &  --       &  --       & 8.3   \\
    \midrule
H\textsubscript{2}O         & Solvent   & Solvent   & Solvent   & Solvent\\
    \bottomrule[1.5pt]
\end{tabularx}
    \end{table}
\end{document}

Also see, if the following solution is usable for you:

enter image description here

Above is used package makecell and its macro thead:

\documentclass{article}
\usepackage{geometry}
\usepackage{booktabs, 
            makecell, % for second example
            tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}}% for vertical centering text in X column
\renewcommand\theadfont{\itshape\normalsize}% for two lines column headers 

...

\begin{tabularx}{\textwidth}{>{\raggedright}Xcccc} % way of defining a fix columns width
    \toprule[1.5pt]
    &   \thead{Resolving gel} 
        &   \thead{Stacking gel} 
            &   \thead{Laemmli\\ sample buffer} 
                &   \thead{Running buffer} \\
    \midrule[1.5pt]
...

for fun:

now, after 2+ years i would redesign this table as follows:

enter image description here

\documentclass{article}
\usepackage{geometry}
\usepackage{booktabs,
            makecell, % for second example
            tabularx}
\renewcommand\theadfont{\itshape\normalsize}% for two lines column headers
\renewcommand\theadgape{}
\usepackage{siunitx}

\begin{document}

    \begin{table}[h]
    \centering
    \sisetup{table-align-text-post=false,
             table-space-text-post={\,\%},
             group-four-digits}
\caption[Material for SDS-PAGE]{Buffer compositions for SDS-PAGE.}
    \label{tab:8899}
\begin{tabularx}{0.8\textwidth}{>{\raggedright}X
                                S[table-format=3.2]
                                S[table-format=3.2]
                                S[table-format=3.5]
                                l
                             }
    \toprule
    &   {\thead{Resolving\\ gel}}
        &   {\thead{Stacking\\ gel}}
            &   {\thead{Laemmli\\ sample buffer}}
                &   \thead{Running\\ buffer} \\
    \midrule
Acrylamide                  & 10 \%     & 5 \%      &  {--}     &  {--} \\
Tris-HCl (1.5 M, pH 8.8)    & 25 \%     &  {--}     &  {--}     &  {--} \\
    \addlinespace
Tris-HCl (1.0 M, pH 6.8)    &  {--}     & 12.5 \%   & 6.25 \%   &  {--} \\
SDS                         & 0.1 \%    & 0.1 \%    &  {--}     &  {--} \\
    \addlinespace
Ammonium persulfate         & 0.1 \%    & 0.1 \%    &  {--}     &  {--} \\
TEMED                       & 0.04 \%   & 0.1 \%    &  {--}     &  {--} \\
    \addlinespace
2-Mercaptoethanol           &  {--}     &  {--}     & 0.1 \%    &  {--} \\
Bromophenol blue            &  {--}     &  {--}     & 0.0005 \% &  {--} \\
    \addlinespace
Glycerol                    &  {--}     &  {--}     & 10 \%     &  {--} \\
TRIS                        &  {--}     &  {--}     &  {--}     & 0.25 M\\
    \addlinespace
Glycine                     &  {--}     &  {--}     &  {--}     & 1.92 M\\
pH                          &  {--}     &  {--}     &  {--}     & 8.3   \\
    \addlinespace
H\textsubscript{2}O         & {Solvent} & {Solvent} & {Solvent} & Solvent\\
    \bottomrule
\end{tabularx}
    \end{table}
\end{document}
3

What you observe is the standard behaviour of tabulars in conjunction with \hlines. There are several solutions, like adding rules of zero width ("struts") that have an appropriate height/depth and keep the horizontal rules at the same distance. A better approach is probably to use the package booktabs and to avoid vertical lines (which are visually bad and unnecessary since the vertical alignment guides the reader anyway).

enter image description here

\documentclass{article}
\usepackage{tabularx}
\usepackage{booktabs}
\begin{document}
\noindent
\begin{tabularx}{\textwidth}{lcX}
\toprule
\textit{} & \textit{A} & \textit{B} \\
\midrule
A & B & C\\
\bottomrule
\end{tabularx}
\end{document}
  • Adding rules with zero width is not really what I want to do. I like the other option. How can I get one of the rules (in my case '\midrule') thicker than normal rules? If I use my previously set code '\thickhline' the inital problem occurs of course again. – Michael Engeli Dec 9 '16 at 8:03
  • @MichaelEngeli The default width of top and bottom rules is \heavyrulewidth, for mid rules it is \lightrulewidth. So if you want to change the defaults completely, modify these lengths. For temporary changes use the optional parameter of the three rule commands that overrides the default; e.g., \midrule[1pt] produces a mid rule of width 1pt. For more information see the documentation of the booktabs package. – gernot says Reinstate Monica Dec 9 '16 at 9:55
3

In addition to what Gernot said: you where comparing normal c and l (and to be complete r) -columns with an X-column. The first three columntypes differ from the last one, in that LaTeX will break long text into multiple lines, if it is a X-column. In fact, the X-column is identical to a p-column of variable width.

You'll have to keep in mind, that the text within a classical l,c or r-column is always placed with its baseline onto the surrounding baseline. This differs from the way, the p, m and b-columns are aligned by LaTeX. Those are also aligned to fit the surrounding baseline. But their "inner" baseline is placed according to their identifying character. The "p"stands for toP. That is, the baseline of the top/first line of that paragraph will be aligned with the surrounding baseline. The m-column stands for "Middle" or centered alignment. The surrounding baseline will therefore pass through the middle of the vertical box height of this paragraph. Last, but not least, the b-column, which is for "Bottom", will merge the baseline of its own last line with the surrounding baseline.

Lets face an example

\documentclass{scrartcl}
\usepackage{array}
\begin{document}
\begin{tabular}{lp{2cm}m{2cm}b{2cm}r}
  foo & long text in the first column
  & long text in the second column
  & even longer text in the third and second-last column
  & bar
\end{tabular}
\end{document}

results in

enter image description here

as you can see, the words "foo" and "bar", which I've placed into the l or r-column, are perfectly aligned to the vertical middle of the middle column.

So, if you want to have the content of your l and c-columns vertically aligned to the middle of X-columns, you'll have to fiddle with LaTeX around.

You could either work with a normal tabular-environment and use a m-column declaration instead of tabularx and its X-column. In that case, you'd have to adapt the width of your b-columns either by hand or set up a clever formulae, to have LaTeX calculate it by its own. (Of course, you could have a glimpse on the tricks, tabularx used to do so on a p-column!)

On the other hand, you might use a clever calculated \raisebox to correct the position of the baseline.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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