在表格中放置和对齐图像

在表格中放置和对齐图像

我需要在表格中对齐一些带有彩色边框的图像,以下是我目前所做的

\documentclass[a4paper,10pt,notitlepage]{article}   % papersize, fontsize, documentclass; 
\usepackage{xltabular}
\usepackage[flushleft]{threeparttablex}     % to have a dedicated notes section after tables
\usepackage[font=small, margin=0pt]{caption}            % table and figure captions
\usepackage[table]{xcolor}                  % used for table alternatively colors
\usepackage{booktabs}                       % use package booktabs for easy creating tables
\usepackage{amssymb}
\usepackage[export]{adjustbox}
\usepackage{array}
\usepackage{cellspace}
\usepackage{multirow}

% Spacing and margin rules
%-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
\setlength{\fboxsep}{0pt}%
\setlength{\fboxrule}{1pt}%
\setlength{\abovecaptionskip}{7pt}
\setlength{\belowcaptionskip}{10pt}
\setlength{\textfloatsep}{5pt}
%---------------------------------------------------------------------------------------------------

\captionsetup[table]{singlelinecheck=false, labelfont={color=black}}  

\begin{document}


\section{Place Images in Table}
\begin{xltabular}{\linewidth}{@{} X|p{0.33\textwidth}|p{0.33\textwidth} @{}}
    \toprule
    \textbf{Text} & \textbf{Image} & \textbf{Image} \\
    \endhead
    \midrule
    This is some text that should be top-aligned in the cell with the image center aligned in it's own cell. Let's make it a little bit longer...\newline I always want a little bit more... & \raisebox{-1.5\height}{\includegraphics[scale=1.0, cfbox=red 2pt 2pt, valign=c]{./content/figures/terminate.png}} & \multirow{2}{*}{\raisebox{-2.5\height}{\includegraphics[scale=1.0, cfbox=green 2pt 2pt, valign=c]{./content/figures/terminate.png}}} \\\cline{1-2}
    This might be a row with some less text & \raisebox{-0.5\height}{\includegraphics[scale=1.0, cfbox=blue 2pt 2pt, valign=c]{./content/figures/terminate.png}} & \\

    \bottomrule
    \caption{Test}
    \label{test}
\end{xltabular}

\end{document}

如何为带有蓝色边框的图像添加最小边距?

这是图片

在此处输入图片描述

答案1

  • 为什么使用xtabular表格环境?
    • 表格是否超过一页?在这种情况下,最好在表格的每页上方添加标题,这样可以清楚地看到表格是从上一页继续的(参见下面的 MWE)
    • 如果表格较短,最好将其括在table浮动中并使用tabularx表格环境。
  • 目前尚不清楚您希望如何在表格中对齐图像:
    • 水平居中(因为图像宽度等于列宽,所以这是默认位置)?
    • 垂直居中?这个不太明确,您能想象应该如何实现吗(如果第一个单元格中的文本只有一行,那么图像应如何相对于这一行定位?
  • 表格中图像放置的代码不清楚且没有提供有用的信息,因此下面是两种可能的解决方案(其中我会使用第一个),它使用\adjustimage命令进行adjustboox放置以及在图像周围绘制框架):
    • 文本和图像顶部对齐
\documentclass[a4paper]{article}           
\usepackage{booktabs, makecell, multirow, xltabular}
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadgape{}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage[flushleft]{threeparttablex}     % to have a dedicated notes section after tables
\usepackage[font=small, margin=0pt]{caption}% table and figure captions
\captionsetup[table]{singlelinecheck=false}
\usepackage[table]{xcolor}                  % used for table alternatively colors
\usepackage{adjustbox}              % it loads graphicx too

% Spacing and margin rules
%---------------------------------------------------------------%
\setlength{\abovecaptionskip}{7pt}
\setlength{\belowcaptionskip}{10pt}
\setlength{\textfloatsep}{5pt}
%---------------------------------------------------------------%

\begin{document}
    \section{Place Images in Table}
\begingroup
\setlength{\fboxsep}{0pt}   % distance of box border from content
\setlength{\fboxrule}{1pt}  % box border thicknes
\adjustboxset{width=\dimexpr\linewidth-\tabcolsep\relax,
              valign=t
              }
\begin{xltabular}{\linewidth}{@{} XXX @{}}
\caption{Test}
\label{test}                \\
    \toprule
\thead{Text} & \thead{Image} & \thead{Image} \\
    \midrule
\endfirsthead
\caption[]{Test (cont.)}    \\
    \toprule
\thead{Text} & \thead{Image} & \thead{Image} \\
    \midrule
\endhead
    \bottomrule
\multicolumn{3}{r}{\footnotesize\textit{continue on the next page}}
\endfoot
    \bottomrule
\endlastfoot
%%%% table body
This is some text that should be top-aligned in the cell with the image center aligned in it's own cell. Let's make it a little bit longer \dots\newline
I always want a little bit more \dots 
    & \adjustimage{cfbox=red}{example-image-duck}
        & \adjustimage{cfbox=blue}{example-image-duck} \\
    \cmidrule{1-2} % why, nicer is use just \midrule
This might be a row with some less text
    & \adjustimage{cfbox=teal}{example-image-duck}
        & 
\end{xltabular}
\endgroup
\end{document}

在此处输入图片描述

  • 文本和图像垂直居中。在这种情况下,在上面的 MWE 中,您需要将\adjustboxsetxltabular规范替换为:
\adjustboxset{width=\dimexpr\linewidth-\tabcolsep\relax,
              valign=c
              }
\begin{xltabular}{\linewidth}{@{} m{0.33\linewidth}CC @{}}% or {@{} m{0.33\linewidth}XX @{}}, result is the same

在此处输入图片描述

附录:

在对齐单元格内容时,您实际上是对齐其内容的基线。在第一个示例中,它们位于每列的顶部,而在第二个示例中,它们位于每列的中间。因此,如果第一列单元格中的文本高于其他列单元格中的图像,则第二个示例解决了您的请求。

如果文本低于图像高度,第二种解决方案也有效,那么您需要在此行中将由从到valign=...定义的图像基线位置更改为并将文本括在第一列中(如果单元格中的文本有多行)。例如,参见下面的 MWE:valign=cvalign=t\parbox

\documentclass[a4paper]{article}
\usepackage{booktabs, makecell, multirow, xltabular}
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadgape{}
\newcolumntype{C}{>{\centering\arraybackslash}X}
\usepackage[flushleft]{threeparttablex}     % to have a dedicated notes section after tables
\usepackage[font=small, margin=0pt]{caption}% table and figure captions
\captionsetup[table]{singlelinecheck=false}
\usepackage[table]{xcolor}                  % used for table alternatively colors
\usepackage{adjustbox}              % it loads graphicx too

\begin{document}
    \section{Place Images in Table}
\begingroup
\setlength{\fboxsep}{0pt}   % distance of box border from content
\setlength{\fboxrule}{1pt}  % box border thicknes
\adjustboxset{width=\dimexpr\linewidth-\tabcolsep\relax,
              valign=c
              }
\begin{xltabular}{\linewidth}{@{} m{0.33\linewidth}XXX @{}}% or {@{} m{0.33\linewidth}XX @{}}, \caption{Test}
\label{test}                \\
    \toprule
\thead{Text} & \thead{Image} & \thead{Image} \\
    \midrule
\endfirsthead
\caption[]{Test (cont.)}    \\
    \toprule
\thead{Text} & \thead{Image} & \thead{Image} \\
    \midrule
\endhead
    \bottomrule
\multicolumn{3}{r}{\footnotesize\textit{continue on the next page}}
\endfoot
    \bottomrule
\endlastfoot
%%%% table body
This is some text that should be top-aligned in the cell with the image center aligned in it's own cell. Let's make it a little bit longer \dots\newline
I always want a little bit more \dots
    & \adjustimage{cfbox=red}{example-image-duck}
        & \adjustimage{cfbox=blue}{example-image-duck} \\
    \midrule
\parbox[t]{\linewidth}{This might be a row with some less text}    % <--- manually adjusted
    & \adjustimage{cfbox=teal,valign=t}{example-image-duck}     % <--- manually adjusted
        &
\end{xltabular}
\endgroup
\end{document}

在此处输入图片描述

上述结果是您想要的吗?

答案2

这是预期的结果吗

--您可以尝试规则宽度(现在设置为 2pt)和填充(设置为 0mm)

    \fboxsep=0mm%padding thickness
    \fboxrule=2pt%border thickness

--第一种颜色(蓝色)附加到图像 fboxrule,第二种颜色(黑色)附加到 fboxsep

在此处输入图片描述

\documentclass[12pt]{article}%
\usepackage{graphicx}
\usepackage{xcolor}
\usepackage{threeparttable}  
\usepackage{booktabs}
\usepackage[export]{adjustbox}  

\fboxsep=0mm%padding thickness
\fboxrule=2pt%border thickness

\begin{document}        
\begin{tabular}{p{1.5in}p{3in}p{1.5in}}\toprule 
AAAAAAAAAAAA AAAAAAAAA&
\fcolorbox{blue}{black}{\includegraphics[width=3in,valign=t]{example-image-a}}&
BBBBBBBBBBB BBBB BBBBBBBBB BBBBBBBBB\\\midrule 
AAAAAAAAAAAA AAAAAAAAA&
\includegraphics[width=3in,valign=t]{example-image-a}&
BBBBBBBBBBB BBBBBB BBBBBBBBB BBBBBBB\\\bottomrule 
\end{tabular}
\end{document}

相关内容