1

enter image description here

In the table above, I want to center align the third row (/word) and the fifth and sixth row so everything looks neat and clean.

\begin{table}[h]
\begin{tabular}{|c|l|}
\hline 
\textbf{man keys} & \multicolumn{1}{c|}{\textbf{What it does?}} \\
\hline
Space & bla bla bla . \\
\hline
Ctrl+F &  bla bla bla  \\
\hline
Ctrl+B & bla bla bla  \\
\hline
/word & bla bla bla \\
\hline
q & bla bla bla  \\ 
\hline
n & bla bla bla \\
\hline
N & bla bla bla \\
\hline
\end{tabular}
\caption{\label{tab:table-name} Doing some stuff}
\end{table}

I tried using multicolumn{1}{m} but it didn't work.

I also don't want to dramatically change the latex script. Is there something I can include in my table to vertically center align cells on demand?

1
  • Do you want to vertically center all cells in the first column? Please edit your minimal example to actually show the alignment issue you have. Currently all cells are already vertically centered since each cell only contains one line. What's the column type of the second column in the screenshot you show? Is this also an l type column? Probably with manual linebreaks?
    – leandriis
    Commented Mar 6, 2020 at 18:17

2 Answers 2

2

You may use a nested tabular with a centred p-column in the second column. Very easy, and LaTeX does all the calculation for you.

I have defined a command \mline that do the work for you.

In this particular MWE, you can also set the second column as anm-column, as example 2 demonstrates. This will not always work.

example 1 - nested tabular

enter image description here

\documentclass{article}

\usepackage{array}
\renewcommand*{\arraystretch}{1.1}
\setlength{\extrarowheight}{2pt}

% Multi-line left-aligned text with manual line breaks.
% The base line is in centre.
\newcommand*{\mline}[1]{%
\begingroup
    \renewcommand*{\arraystretch}{1.1}%
   \begin{tabular}[c]{@{}>{\raggedright\arraybackslash}p{2cm}@{}}#1\end{tabular}%
  \endgroup
}

\begin{document}

\begin{table}[tbh!]
\centering
\begin{tabular}{|c|l|}
\hline 
\bfseries man keys & \multicolumn{1}{c|}{\bfseries What it does?} \\
\hline
Space & \mline{bla bla bla blabla ltus blabla.} \\
\hline
Ctrl+F &  \mline{bla bla bla}  \\
\hline
Ctrl+B & \mline{bla bla bla}  \\
\hline
/word & \mline{much more bla bla bla} \\
\hline
q & \mline{I hate this bla bla bla } \\ 
\hline
n & \mline{so much bla bla bla} \\
\hline
N & \mline{all this bla bla bla} \\
\hline
\end{tabular}
\caption{\label{tab:table-name} Doing some stuff}
\end{table}

\end{document}

Example 2 - m-column

enter image description here

\documentclass{article}

\usepackage{array}
\renewcommand*{\arraystretch}{1.1}
\setlength{\extrarowheight}{2pt}

\begin{document}


\begin{table}[tbh!]
\centering
\begin{tabular}{|c|>{\raggedright\arraybackslash}m{2.5cm}|}
\hline 
\bfseries man keys & \multicolumn{1}{c|}{\bfseries What it does?} \\
\hline
Space & bla bla bla blabla ltus blabla.\\
\hline
Ctrl+F &  bla bla bla  \\
\hline
Ctrl+B & bla bla bla  \\
\hline
/word & much more bla bla bla \\
\hline
q & I hate this bla bla bla  \\ 
\hline
n & so much bla bla bla \\
\hline
N & all this bla bla bla \\
\hline
\end{tabular}
\caption{\label{tab:table-name-ii} Doing some more stuff}
\end{table}

\end{document}
2
  • Wouldn't it be easier to just use an m type column for the second column?
    – leandriis
    Commented Mar 6, 2020 at 19:25
  • @leandriis See example 2. However, m-columns do not always work as expected. Therefore, two solutions.
    – Sveinung
    Commented Mar 6, 2020 at 19:51
1

The simplest method is with \multirow{number of rows}{width}{text}.

A MWE follows.

\documentclass{article}
\usepackage{multirow}
\usepackage{tabularx}
\begin{document}
    \begin{table}[h]
        \begin{tabular}{|c|l|}
            \hline
              \textbf{man keys}    & \multicolumn{1}{c|}{\textbf{What it does?}} \\
            \hline
                    Space          & bla bla bla .                               \\
            \hline
                    Ctrl+F         & bla bla bla                                 \\
            \hline
                    Ctrl+B         & bla bla bla                                 \\
            \hline
            \multirow{2}{*}{/word} & bla bla bla                                 \\
            \cline{2-2}
                                   & foo foo foo                                 \\
            \hline
                      q            & bla bla bla                                 \\
            \hline
                      n            & bla bla bla                                 \\
            \hline
                      N            & bla bla bla                                 \\
            \hline
        \end{tabular}
        \caption{\label{tab:table-name} Doing some stuff}
    \end{table}
\end{document}

enter image description here

3
  • Are you sure this is what OP asks for? His/hers example shows a multiline second column, not two rows.
    – Sveinung
    Commented Mar 6, 2020 at 19:25
  • I agree your answer @Sveinung is much more suitable than mine (specially your example 2). OP's example shows half a table and I didn't know if OP was looking for multilines or multirows. Should I remove my answer because it may be misleading or should I keep it as an alternative that produces a similar visual output?
    – FHZ
    Commented Mar 7, 2020 at 0:24
  • No reason to delete it, in my opinion.
    – Sveinung
    Commented Mar 7, 2020 at 9:01

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