我想使用 tcolorbox 创建示例框,如下图所示:
到目前为止我得到的
这是我的代码:
\documentclass[justified]{tufte-book}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\newtcolorbox[auto counter, number within=chapter]{example}[1][]{
enhanced,
breakable,
left=0.5em,right=0pt,top=1pt,bottom=1pt,
attach title to upper={},
coltitle={black},
fonttitle={\bfseries},
title={EXAMPLE \thetcbcounter\ },
sharp corners,
frame hidden,
boxrule=0pt,
borderline north={1pt}{0pt}{gray},
borderline west={1pt}{0pt}{blue},
borderline south={1pt}{0pt}{red},
colback={white},
#1
}
\newcommand\solution{\par\textsf\bfseries{\textbf{\textcolor{black}{SOLUTION }}}}
\begin{document}
\begin{example}
\lipsum[2]
\solution \lipsum[4]
\end{example}
\end{document}
我希望北边界线的长度与“示例 0.1”文本一样长,而南边界线的长度只是任意水平线。
有人能改进我的代码来创建与我上面发布的相同的图片吗?谢谢!
答案1
除非您使用boxed title
,否则没有节点title
可用作参考。因此,我已将您的样式更改为使用 ,而不是在选项之前boxed title
插入。这样就可以在标题框上绘制线条。框下方的线条是用命令添加的。hspace
attach title to upper
overlay
\documentclass[justified]{tufte-book}
\usepackage[most]{tcolorbox}
\usepackage{lipsum}
\newtcolorbox[auto counter, number within=chapter]{example}[1][]{
enhanced,
breakable,
left=0.5em, right=0pt, top=1pt, bottom=1pt,
attach boxed title to top left={yshift=-\tcboxedtitleheight},
boxed title style={%
empty,
right=0pt,
frame code={\draw[line width=1pt, gray] (frame.north west)--(frame.north east);}},
before upper=\hspace{\tcboxedtitlewidth},
colbacktitle=white,
coltitle={black},
colback={white},
fonttitle={\bfseries},
title={EXAMPLE \thetcbcounter\ },
sharp corners,
frame hidden,
boxrule=0pt,
borderline west={1pt}{0pt}{blue},
overlay unbroken and last={%
\draw[line width=1pt, red] (frame.south west) -- ++(0:1cm);},
#1
}
\newcommand\solution{\par\textsf\bfseries{\textbf{\textcolor{black}{SOLUTION }}}}
\begin{document}
\begin{example}
\lipsum[2]
\solution \lipsum[4]
\end{example}
\end{document}