在 tcolorbox 中使用 multicol 段落內奇怪的垂直間距

在 tcolorbox 中使用 multicol 段落內奇怪的垂直間距

基本想法是將標題放在圖像旁邊並將其垂直居中。我在一個環境中嘗試了這個,tcolorbox因為它被用在海報上。我不確定框架有多tcolorbox重要,但我將它保留在這裡作為容器。

這個想法是將圖片放入一列,將標題放入另一列,並應使用\vspace命令進行偏移。如下所示,程式碼將垂直空間放置在文字的第一行和第二行之間,而不是在第一行上方(程式碼中的位置)。

\documentclass{standalone}
\usepackage{tikz}
\usepackage{multicol}
\usepackage{tcolorbox}

\begin{document}

\begin{tcolorbox}[boxsep=-1mm]
\begin{multicols}{2} % also tried unbalanced multicols*
\begin{tikzpicture}
    \draw (0,0) -- (5,0) -- (5,4) -- (0,4) -- (0,0);
    \draw (0,0) -- (5,4);
    \draw (5,0) -- (0,4);
\end{tikzpicture}
\vspace{0.5cm} % this command causes strange vertical spacing
Figure aption. Weird vertical spacing between 1st and 2nd line, when using \textbackslash vspace command.
\end{multicols}
\end{tcolorbox}

\end{document}

為了進行比較,您可以看到兩張圖片,顯示了該指令的效果\vspace

沒有 vspace 指令的正確行為 使用 vspace 指令時奇怪的垂直間距

有辦法繞過這個嗎?這是一個錯誤還是一個功能?

答案1

您必須horizontal先離開該模式,即使用空行,然後\vspace{0.5cm}在確實需要時發出問題。

\documentclass{standalone}
\usepackage{tikz}
\usepackage{multicol}
\usepackage{tcolorbox}

\begin{document}

\begin{tcolorbox}[boxsep=-1mm]
\begin{multicols}{2} % also tried unbalanced multicols*
\begin{tikzpicture}
    \draw (0,0) -- (5,0) -- (5,4) -- (0,4) -- (0,0);
    \draw (0,0) -- (5,4);
    \draw (5,0) -- (0,4);
\end{tikzpicture}



% this command causes strange vertical spacing
\vspace{0.5cm}
Figure caption. No weird vertical spacing between 1st and 2nd line, when using \textbackslash vspace command.
\end{multicols}
\end{tcolorbox}

\end{document}

在此輸入影像描述

答案2

如果使用的話tcolorbox不需要multicol環境,因為tcolorbox已經將每個盒子分成了upperlower部分,可以並排放置。

獲得OP所需結果的最簡單方法可能是\tcbsidebysidebox(自2015-11-20版本以來就存在),它需要xparsetcolorboxlibrary。此命令已將框分成相等的兩列,並且內容垂直居中:

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\usepackage[skins,xparse]{tcolorbox}

\begin{document}

\tcbsidebyside[notitle,lower separated=false]{%
\begin{tikzpicture}
    \draw (0,0) -- (5,0) -- (5,4) -- (0,4) -- (0,0);
    \draw (0,0) -- (5,4);
    \draw (5,0) -- (0,4);
\end{tikzpicture}
}{%
Figure caption. Weird vertical spacing between 1st and 2nd line, when using \textbackslash vspace command.
}
\end{document}

在此輸入影像描述

相關內容