垂直相邻的 tcolorboxes

垂直相邻的 tcolorboxes

有人问过类似的问题这里。我要求的有些不同:有一个命令使两个 tcolorbox 垂直相邻。我的尝试是:

\begin{tcolorbox}
\end{tcolorbox}

\vspace{-1.025cm}

\begin{tcolorbox}
\end{tcolorbox}

(下面给出了一个完整的最小示例。)最终结果将看起来像一个表格,水平线将“单元格”(tcolorboxes 的内部)分开。但是,我实际上并不想将其变成表格。有时 tcolorboxes 不会相邻(它们之间会有文本),因此最好有让它们重叠的选项,但并非总是如此。

以下是完整示例:

\documentclass{report}
\usepackage{tcolorbox}

\newlength{\normalparindent}
\AtBeginDocument{\setlength{\normalparindent}{\parindent}}
\tcbset{before upper={\setlength{\parindent}{\normalparindent}}}

\tcbuselibrary{skins}
\tcbset{
    enhanced,
    frame hidden,interior hidden,
    sharp corners,
    boxrule=0pt,
    left=-0.1cm,right=-0.1cm,top=0.25cm,bottom=0.25cm,
    toptitle=0.25cm+1pt,
    bottomtitle=0.0cm+1pt,
    colframe=white,colback=white,coltitle=black,
    title style=white,
    bottomrule=1pt,
    borderline north={1pt}{0pt}{lightgray},
    borderline south={1pt}{0pt}{lightgray},
    fonttitle=\bfseries,fontupper=\normalsize,
    before skip=0.375cm+2pt,after skip=0.475cm+2pt
}

\begin{document}

\begin{tcolorbox}[title=1.1\hspace{1.0em}Definitions]
\noindent Content.
\end{tcolorbox}
\vspace{-1.025cm}
\begin{tcolorbox}[title=1.2\hspace{1.0em}Theorem]
\noindent Content.
\end{tcolorbox}

\end{document}

我对这个解决方案不满意,因为重叠的边框打印出来比不重叠的边框颜色深。有人知道有什么好办法吗?

答案1

这里我使用raster库来tcolorbox创建一个环境mycells。在这个环境之外,你的框与你定义的一样。在环境内部,重叠的线会自动移除,并且垂直距离会进行调整:

\documentclass{report}
\usepackage{tcolorbox}

\newlength{\normalparindent}
\AtBeginDocument{\setlength{\normalparindent}{\parindent}}
\tcbset{before upper={\setlength{\parindent}{\normalparindent}}}

\tcbuselibrary{skins,raster}
\tcbset{
    enhanced,
    frame hidden,interior hidden,
    sharp corners,
    boxrule=0pt,
    left=-0.1cm,right=-0.1cm,top=0.25cm,bottom=0.25cm,
    toptitle=0.25cm+1pt,
    bottomtitle=0.0cm+1pt,
    colframe=white,colback=white,coltitle=black,
    %title style=white,% would actually draw a white box
    bottomrule=1pt,
    borderline north={1pt}{0pt}{lightgray},
    borderline south={1pt}{0pt}{lightgray!10!red},
    fonttitle=\bfseries,fontupper=\normalsize,
    before skip=0.375cm+2pt,after skip=0.475cm+2pt
}

\newenvironment{mycells}[1][]{%
  \begin{tcbraster}[
    raster columns=1,
    raster before skip=0.375cm+2pt,
    raster after skip=0.475cm+2pt,
    no borderline,
    borderline south={1pt}{0pt}{lightgray},
    raster row 1/.style={borderline north={1pt}{0pt}{lightgray}},
    raster row skip=-1pt,
    #1
  ]}{\end{tcbraster}}

\begin{document}

\begin{mycells}
  \begin{tcolorbox}[title=1.1\hspace{1.0em}Definitions]
  \noindent Content.
  \end{tcolorbox}
  \begin{tcolorbox}[title=1.2\hspace{1.0em}Theorem ]
  \noindent Content.
  \end{tcolorbox}
\end{mycells}

\end{document}

在此处输入图片描述

答案2

我不知道这段代码是如何转换为 pdf 并最终打印出来的,但如果打印了两行,你总是可以定义tcolorbox仅重叠borderline north并添加borderline south到最后一行。

\documentclass{report}
\usepackage{tcolorbox}

\newlength{\normalparindent}
\AtBeginDocument{\setlength{\normalparindent}{\parindent}}
\tcbset{before upper={\setlength{\parindent}{\normalparindent}}}

\tcbuselibrary{skins}
\tcbset{
    enhanced,
    frame hidden,interior hidden,
    sharp corners,
    boxrule=0pt,
    left=-0.1cm,right=-0.1cm,top=0.25cm,bottom=0.25cm,
    toptitle=0.25cm+1pt,
    bottomtitle=0.0cm+1pt,
    colframe=white,colback=white,coltitle=black,
    title style=white,
    bottomrule=1pt,
    borderline north={1pt}{0pt}{lightgray},
%    borderline south={1pt}{0pt}{lightgray},
    fonttitle=\bfseries,fontupper=\normalsize,
    before skip=0.375cm+2pt,after skip=0.475cm+2pt
}

\begin{document}

\begin{tcolorbox}[title=1.1\hspace{1.0em}Definitions]
\noindent Content.
\end{tcolorbox}
\vspace{-1.025cm}
\begin{tcolorbox}[title=1.2\hspace{1.0em}Theorem,borderline south={1pt}{0pt}{lightgray} ]
\noindent Content.
\end{tcolorbox}

\end{document}

在此处输入图片描述

相关内容