如何将 parbox 垂直居中放置在表格中

如何将 parbox 垂直居中放置在表格中

我创建了一个包含图像的表格。但是我希望第一列包含文本,并且文本应垂直居中,并且文本应旋转 90 度。为此,我使用了 parbox,但是似乎无法将其垂直居中。有人知道我该怎么做吗?

\begin{table*}[h!]
     \begin{center}
        %  \begin{tabular}{ p{1cm}  p{5cm}  p{5cm}  p{5cm}}
         \begin{tabular}{c c c c}
             \toprule
              Planner & Full Communication & Partial Communication & Poor Communication \\ 
              \cmidrule(r){1-1}\cmidrule(lr){2-2}\cmidrule(l){3-3}\cmidrule(l){4-4} % the bar under the col title
              {\centering
              \parbox[t]{4mm}{\rotatebox[origin=c]{90}{MCTS (Greedy) CNN}}
              }
              & 
             \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              & 
              \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              &
             \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              \\
             \bottomrule
             
              A             
              & 
             \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              & 
              \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              &
             \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              \\
             \bottomrule
             
              A             
              & 
             \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              & 
              \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              &
             \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              \\
             \bottomrule
             
              A             
              & 
             \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              & 
              \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              &
             \raisebox{-\totalheight}{\includegraphics[width=0.3\textwidth, height=50mm]{images/sample.png}}
              \\
             \bottomrule
             
          \end{tabular}
          \caption{my.Lboro Analysis}
          \label{tbl:myLboro}
      \end{center}
  \end{table*}



此代码生成的结果

答案1

像这样?

在此处输入图片描述

  • 我们没有关于您文档布局的任何信息,因此请始终提供 MWE(最小工作示例),这是一个完整的小文档,其中包含现在丢失的信息
  • 不清楚所需的表格布局)第一列中的所有单元格是否都有旋转的文本,或者如您的代码片段所示
  • 通过使用adjustboxrotating和包makecelltabularray可以编写简洁清晰的表格代码:
\documentclass{article}
\usepackage[export]{adjustbox}
\usepackage{rotating}
\usepackage{makecell}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
    \begin{table}[ht]
    \settowidth\rotheadsize{MCTS (Greedy)}
    \adjustboxset{width=\linewidth, height=33mm,
                  valign=M}
\begin{tblr}{colsep = 3pt,
             colspec = {@{} Q[c] X[c] X[c] X[c] @{}},
             rowsep = 3pt,
             row{1} = {font=\bfseries, c, m},
            }
     \toprule
Planner 
    & Full Communication 
        & Partial Communication 
            & Poor Communication                        \\
    \cmidrule[r]{1-1}
    \cmidrule[lr]{2-2}\cmidrule[l]{3-3}\cmidrule[l]{4-4} 
\SetCell{cmd=\rotcell[cc]}   {MCTS (Greedy)\\ CNN} 
    &   \adjincludegraphics{example-image-duck}
        &   \adjincludegraphics{example-image-duck}
            &   \adjincludegraphics{example-image-duck}    \\
    \midrule
A
    &   \adjincludegraphics{example-image-duck}
        &   \adjincludegraphics{example-image-duck}
            &   \adjincludegraphics{example-image-duck}    \\
    \midrule
B
    &   \adjincludegraphics{example-image-duck}
        &   \adjincludegraphics{example-image-duck}
            &   \adjincludegraphics{example-image-duck}    \\

    \midrule
C
    &   \adjincludegraphics{example-image-duck}
        &   \adjincludegraphics{example-image-duck}
            &   \adjincludegraphics{example-image-duck}    \\
    \bottomrule
\end{tblr}
\caption{my.Lboro Analysis}
\label{tbl:myLboro}
    \end{table}
\end{document}

附录: 您可能喜欢一些不同的列标题,其中没有重复的列内容信息:

在此处输入图片描述

为此,需要对上述 MWE 进行以下更改:

\documentclass{article}
\usepackage[export]{adjustbox}
\usepackage{rotating}
\usepackage{makecell}
\usepackage{tabularray}
\UseTblrLibrary{booktabs}

\begin{document}
    \begin{table}[ht]
    \settowidth\rotheadsize{MCTS (Greedy)}
    \adjustboxset{width=\linewidth, height=33mm,
                  valign=M}
\begin{tblr}{colsep = 3pt,
             colspec = {@{} Q[c,h] X[c] X[c] X[c] @{}},
             rowsep = 3pt,
             row{1,2} = {font=\bfseries, c, m},
            }
     \toprule
\SetCell[r=2]{c}   Planner 
    & \SetCell[c=3]{c}Communication 
        &   &                                               \\
    \midrule
    & Full 
        & Partial 
            & Poor                                          \\
    \midrule
\SetCell{cmd=\rotcell[cc]}   {MCTS (Greedy)\\ CNN} 
    &   \adjincludegraphics{example-image-duck}
        &   \adjincludegraphics{example-image-duck}
            &   \adjincludegraphics{example-image-duck}    \\
    \midrule
% rest of table code

答案2

这使用了 adjustbos/graphicx 的 [valign=...] 功能。

\documentclass[twocolumn,a4paper]{article}
\usepackage[export]{adjustbox}
\usepackage{graphicx}
\usepackage{booktabs}

\begin{document}

\begin{table*}[tp]% table* does not support [h]
     \centering
     %\setkeys{Gin}{valaign=M}% doesn't work
        %  \begin{tabular}{ p{1cm}  p{5cm}  p{5cm}  p{5cm}}
         \begin{tabular}{c c c c}
             \toprule
              Planner & Full Communication & Partial Communication & Poor Communication \\ 
              \cmidrule(r){1-1}\cmidrule(lr){2-2}\cmidrule(l){3-3}\cmidrule(l){4-4} % the bar under the col title
              \rotatebox[origin=c]{90}{MCTS (Greedy) CNN}
              & 
             \includegraphics[width=0.3\textwidth, height=50mm, valign=M]{example-image}
              & 
             \includegraphics[width=0.3\textwidth, height=50mm, valign=M]{example-image}
              &
             \includegraphics[width=0.3\textwidth, height=50mm, valign=M]{example-image}\llap{\rule{5cm}{0.5pt}}
              \\
             \bottomrule
             
              A             
              & 
             \includegraphics[width=0.3\textwidth, height=50mm, valign=t]{example-image}
              & 
             \includegraphics[width=0.3\textwidth, height=50mm, valign=t]{example-image}
              &
             \includegraphics[width=0.3\textwidth, height=50mm, valign=t]{example-image}
              \\
             \bottomrule
             
              A             
              & 
             \includegraphics[width=0.3\textwidth, height=50mm, valign=t]{example-image}
              & 
             \includegraphics[width=0.3\textwidth, height=50mm, valign=t]{example-image}
              &
             \includegraphics[width=0.3\textwidth, height=50mm, valign=t]{example-image}
              \\
             \bottomrule
             
              A             
              &
             \includegraphics[width=0.3\textwidth, height=50mm, valign=t]{example-image}
              & 
             \includegraphics[width=0.3\textwidth, height=50mm, valign=t]{example-image}
              &
             \includegraphics[width=0.3\textwidth, height=50mm, valign=t]{example-image}
              \\
             \bottomrule
             
          \end{tabular}
          \caption{my.Lboro Analysis}
          \label{tbl:myLboro}
  \end{table*}
  
  \end{document}

相关内容