带有水平分隔标题和副标题的 Tcolorbox

带有水平分隔标题和副标题的 Tcolorbox

我想做一个tcolorbox大致像这样的

-----------------------------------------------------------------------------------
            |          Subtitle (If the subtitle is too long, like it is here, 
TITLE       |          then it should wrap like this, and the title on the left
            |          hand side should be centered relative to the subtitle)
-----------------------------------------------------------------------------------
Here is the content of the tcolorbox.
-----------------------------------------------------------------------------------

线条应为实线且连贯,垂直对齐应如上所述。应可以指定从左边距到水平分隔符的距离,以及从水平分隔符到字幕文本块的距离(这将用于所有可能tcolorboxes具有不同长度标题的 。应可以独立指定用于标题和字幕的字体。

这是一个简单的例子:

\documentclass{report}
\usepackage{tcolorbox}

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

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

\begin{document}

\begin{tcolorbox}[title=TITLE]
\noindent Content.
\end{tcolorbox}

\end{document}

答案1

类似这样的事?

在此处输入图片描述

在我的示例代码中,我创建了一个新环境mybox,它将tcolorbox选项作为可选参数,然后将标题和副标题文本作为普通参数。

新的长度设置是通过

  my rule=1pt,
  left to separator=3cm,
  separator to subtitle=1cm,

其中my rule存储规则宽度,另外两个选项存储您要求的到垂直线的距离。

\documentclass{report}
\usepackage{tcolorbox,lipsum}
\tcbuselibrary{skins,raster,breakable}

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

\tcbset{
  my rule/.store in=\myrule,
  left to separator/.store in=\mylefttoseparator,
  separator to subtitle/.store in=\myseparatortosubtitle,
}

\newtcolorbox{mybox}[3][]{%
  my rule=1pt,
  left to separator=3cm,
  separator to subtitle=1cm,
  enhanced,
  frame hidden,interior hidden,
  sharp corners,
  boxrule=\myrule,boxsep=0pt,
  left=0pt,right=0pt,top=0.35cm,bottom=0.35cm,
  toptitle=0.35cm,bottomtitle=0.35cm,
  colframe=black,colback=white,coltitle=black,
  borderline north={\myrule}{0pt}{black},
  borderline south={\myrule}{0pt}{black},
  fonttitle=\bfseries,fontupper=\normalsize,
  before skip=0.375cm+2pt,after skip=0.475cm+2pt,
  lefttitle=\mylefttoseparator+\myrule+\myseparatortosubtitle,
  title={\raggedright\mbox{}#3},
  underlay unbroken and first={
    \node[right,inner sep=0pt,outer sep=0pt,
      font=\large\bfseries,% <-- title font
      text width=\mylefttoseparator-5mm] at (title.west) {#2};
    \draw[tcbcol@frame,line width=\myrule]
      ([xshift=-\myrule,yshift=-\myrule/2]title.south west)--([xshift=\myrule,yshift=-\myrule/2]title.south east)
      ([xshift=\mylefttoseparator+\myrule/2]title.south west)--([xshift=\mylefttoseparator+\myrule/2]title.north west);
  },
  #1,
}


\begin{document}

\begin{mybox}{TITLE}{SUBTITLE}
\noindent\lipsum[2]
\end{mybox}

\begin{mybox}{TITLE}{Subtitle (If the subtitle is too long, like it is here,
     then it should wrap like this, and the title on the left
     hand side should be centered relative to the subtitle)}
\noindent\lipsum[3]
\end{mybox}

\end{document}

相关内容