tcolorbox/minipage:如何自动将图像高度设置为其父图像的高度?

tcolorbox/minipage:如何自动将图像高度设置为其父图像的高度?

对于以下内容,我需要

1- 自动将每幅图像的高度设置为父图像的高度minipage/tcolorbox而不是手动设置height=2cm

2- 了解为什么example-image-a小页面内部会超出右边距

在此处输入图片描述

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}
\usepackage[most]{tcolorbox}
\usepackage[showframe]{geometry}

\begin{document}

\noindent
\setlength{\fboxsep}{0pt}%
\fbox{%
    \begin{minipage}[t][2cm][c]{\dimexpr\textwidth/3}
        First line\\ Second Line
    \end{minipage}}%
\fbox{%
    \begin{minipage}[t][2cm][c]{\dimexpr\textwidth/3}
        \centering Centered Text
    \end{minipage}}%
\fbox{%
    \begin{minipage}[t][2cm][c]{\dimexpr\textwidth/3}
        \flushright \includegraphics[height=2cm]{example-image-a}
    \end{minipage}}%


\vspace{2\baselineskip}

\begin{tcbraster}[
        raster columns=3, raster rows=1,
        raster height = 2cm, sharp corners,
        valign=center,
        raster column skip = 0mm,
        boxrule = 0.5pt, colback = white,
        boxsep=0mm, left=0pt, right=0pt
    ]
    \begin{tcolorbox}[left=0pt,right=0pt]
        First line\\ Second Line
    \end{tcolorbox}
    \begin{tcolorbox}[halign=center]
        Centered Text
    \end{tcolorbox}
    \begin{tcolorbox}[halign=right]
        \includegraphics[height=2cm]{example-image-b}
    \end{tcolorbox}
\end{tcbraster}

\end{document}

答案1

您忘记减去边框的长度: \fboxrule(左边框)+ \fboxrule(右边框)。因此,单元格的长度为 \textwidth/3 - 2\fboxrule

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}
\usepackage[showframe]{geometry}

\begin{document}

% \setlength{\fboxrule}{2pt} % <-- choose the thickness of the border
\setlength{\fboxsep}{0pt}
\noindent
\fbox{%
    \begin{minipage}[t][2cm][c]{\dimexpr\textwidth/3-2\fboxrule\relax}
        First line\\ Second Line
    \end{minipage}}%
\fbox{%
    \begin{minipage}[t][2cm][c]{\dimexpr\textwidth/3-2\fboxrule\relax}
        \centering Centered Text
    \end{minipage}}%
\fbox{%
    \begin{minipage}[t][2cm][c]{\dimexpr\textwidth/3-2\fboxrule\relax}
        \raggedleft \includegraphics[height=2cm]{example-image-a}
    \end{minipage}}

\end{document}

相关内容