水平和垂直对齐int表格单元格

时间:2014-08-06 15:06:53

标签: latex

我到目前为止看到的大多数迹象似乎表明m用于垂直居中细胞内容,但它对我不起作用。这就是我所拥有的

\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

我希望左边的单元格垂直居中,而顶部的单元格水平居中。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:4)

看起来很奇怪,您需要在不希望居中的列上指定m - 列。这里的动机是m - 列在单元格的中间(垂直)设置锚点,就像p - 列设置锚点(再次,垂直)在第一行的基线处。因此,将第二列和第三列(垂直较高)设置为m - 列将相对于其他列垂直居中。

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}

我在编码风格方面做了一些小调整,所有这些都是为了更加一致的使用和易于改变,如果将来发生的话。也就是说,我删除了一些硬编码的\textbullet和间距内容,并将其替换为可以全局更改的环境,如果需要的话。

答案 1 :(得分:0)

这就是你要找的东西

  

table wrap

代码如下

\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}