Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Horizontal and vertical alignment in tabular cell

Tags:

latex

tabular

Most indications I've seen so far seem to indicate that m is used to center cell contents vertically, but it's not working for me. Here's what I have

\begin{table}[htb]
\centering

    \sffamily \begin{tabularx}{1.0\textwidth}{ m{3cm} p{5.5cm} p{5.5cm} }
    \hline
    & 
    \textbf{Helpful}
    &
    \textbf{Harmful}
    \hfill \\ \hline

    \textbf{Internal origin} \\ (organization) &
    Item 1
    ~\textbullet~ Item 2
    ~\textbullet~ Item 3
    ~\textbullet~ Item 4
    ~\textbullet~ Item 5
    ~\textbullet~ Item 6
    ~\textbullet~ Item 7
    ~\textbullet~ Item 8
    ~\textbullet~ Item 9

    &
    Item 1
    ~\textbullet~ Item 2
    ~\textbullet~ Item 3
    ~\textbullet~ Item 4
    ~\textbullet~ Item 5
    ~\textbullet~ Item 6
    ~\textbullet~ Item 7
    ~\textbullet~ Item 8
    ~\textbullet~ Item 9

    \\
    \hline
    \textbf{External origin} \\ (environment) &
    Item 1
    ~\textbullet~ Item 2
    ~\textbullet~ Item 3
    ~\textbullet~ Item 4
    ~\textbullet~ Item 5
    ~\textbullet~ Item 6
    ~\textbullet~ Item 7
    ~\textbullet~ Item 8
    ~\textbullet~ Item 9

    &
    Item 1
    ~\textbullet~ Item 2
    ~\textbullet~ Item 3
    ~\textbullet~ Item 4
    ~\textbullet~ Item 5
    ~\textbullet~ Item 6
    ~\textbullet~ Item 7
    ~\textbullet~ Item 8
    ~\textbullet~ Item 9

    \\
    \hline
    \end{tabularx} \normalfont
\caption{SWOT matrix}
\label{tab:swot-matrix}
\end{table}

enter image description here

I want the left cells to be centred vertically and the top ones horizontally. How can I achieve this?

like image 488
André Fratelli Avatar asked Aug 06 '14 15:08

André Fratelli


2 Answers

Strange as it may seem, you need to specify an m-column on the columns that you don't want centred. The motivation here is that the m-column sets an anchor in the middle (vertically) of the cell just like a p-column sets the anchor (again, vertically) at the baseline of the first line. So, setting the second and third column (vertically taller) as m-columns would vertically centre them with regards to the other column(s).

enter image description here

\documentclass{article}
\usepackage[margin=1cm]{geometry}% Just for this example
\usepackage{tabularx,array,booktabs}
\newenvironment{shortlist}
  {\renewcommand{\item}{\renewcommand{\item}{\unskip\space\textbullet~}}}
  {}
\renewcommand{\tabularxcolumn}[1]{m{#1}}

\begin{document}
\noindent\sffamily
\begin{tabularx}{\textwidth}{ m{3cm} X X }
  \toprule
  & \multicolumn{1}{c}{\textbf{Helpful}} & \multicolumn{1}{c}{\textbf{Harmful}} \\
  \midrule  
  \textbf{Internal origin} \\ (organization) &
  \begin{shortlist}
    \item Item 1 \item Item 2 \item Item 3 \item Item 4 \item Item 5
    \item Item 6 \item Item 7 \item Item 8 \item Item 9
  \end{shortlist}
  &
  \begin{shortlist}
    \item Item 1 \item Item 2 \item Item 3 \item Item 4 \item Item 5
    \item Item 6 \item Item 7 \item Item 8 \item Item 9
  \end{shortlist}
  \\
  \textbf{External origin} \\ (environment) &
  \begin{shortlist}
    \item Item 1 \item Item 2 \item Item 3 \item Item 4 \item Item 5
    \item Item 6 \item Item 7 \item Item 8 \item Item 9
  \end{shortlist}
  &
  \begin{shortlist}
    \item Item 1 \item Item 2 \item Item 3 \item Item 4 \item Item 5
    \item Item 6 \item Item 7 \item Item 8 \item Item 9
  \end{shortlist}
  \\
  \bottomrule
\end{tabularx}
\end{document}

I've made some small adjustments in terms of coding style, all geared towards more consistent usage and ease-of-change, if this is to happen in the future. That is, I removed some of the hard-coded \textbullet and spacing stuff and replaced them with environments that can be changed globally, if need be.

like image 188
Werner Avatar answered Nov 02 '22 05:11

Werner


Is this what you are looking for

table wrap

The code is as follows

\begin{table}[h]
\centering
\sffamily \begin{tabularx}{1\textwidth}{>{\centering\arraybackslash}m{3cm}|>{\centering\arraybackslash}m{3.7cm}|>{\centering\arraybackslash}m{3.7cm}}
\hline
~ & \textbf{Helpful} & \textbf{Harmful} \\
\hline
\textbf{Internal origin} (organization) & Item 1 ~\textbullet~ Item 2 ~\textbullet~ Item 3 ~\textbullet~ Item 4 ~\textbullet~ Item 5 ~\textbullet~ Item 6 ~\textbullet~ Item 7 ~\textbullet~ Item 8 ~\textbullet~ Item 9 & Item 1 ~\textbullet~ Item 2 ~\textbullet~ Item 3 ~\textbullet~ Item 4 ~\textbullet~ Item 5 ~\textbullet~ Item 6 ~\textbullet~ Item 7 ~\textbullet~ Item 8 ~\textbullet~ Item 9 \\
\hline
\textbf{External origin} (environment) & Item 1 ~\textbullet~ Item 2 ~\textbullet~ Item 3 ~\textbullet~ Item 4 ~\textbullet~ Item 5 ~\textbullet~ Item 6 ~\textbullet~ Item 7 ~\textbullet~ Item 8 ~\textbullet~ Item 9 & Item 1 ~\textbullet~ Item 2 ~\textbullet~ Item 3 ~\textbullet~ Item 4 ~\textbullet~ Item 5 ~\textbullet~ Item 6 ~\textbullet~ Item 7 ~\textbullet~ Item 8 ~\textbullet~ Item 9 \\
\hline
\end{tabularx} \normalfont
\caption{SWOT matrix}
\label{tab:swot-matrix}
\end{table}
like image 29
zyy Avatar answered Nov 02 '22 05:11

zyy