使用 tcolorbox 创建自定义笔记环境

使用 tcolorbox 创建自定义笔记环境

我刚刚偶然发现了tcolorbox包,但似乎无法找到它的广泛内容手动的

我想要实现的是创建一个自定义环境,以便将简短的注释添加到正文中。所附段落应以单词“笔记:' 以粗体显示并出现在与周围文本左边距齐平的垂直线旁边,如下所示:

在此处输入图片描述

到目前为止,我一直在使用这个mdframed软件包,但我开始对它频繁出现的警告感到厌烦

您遇到了一个糟糕的情况,因为最后一个框是空的,您必须通过更改文本、空格或其他内容来手动更改它。

这是迄今为止我一直使用的代码。

\documentclass[11pt]{scrartcl}

\usepackage[framemethod=tikz]{mdframed}
\newmdenv[
    topline=false,
    bottomline=false,
    rightline=false,
    innerrightmargin=0pt
]{siderule}
\newenvironment{note}%
    {\begin{siderule}\textbf{Note:}}
    {\end{siderule}}

\usepackage{blindtext}

\begin{document}

\blindtext
\begin{note}
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}
\blindtext

\end{document}

我希望有人可以帮助简化它并使它更加健壮tcolorbox

答案1

这样的框很容易用borderline选项来绘制垂直条(或其他东西)

由于note环境应该是多功能的,因此可能有一个可选参数,可以在需要时设置其他选项,请参阅环境的第二个(丑陋!)示例note

left=...可以使用“等”和“等”键来更改框前/后和框内部的垂直等间距before skip={}

\documentclass[11pt]{article}

\usepackage[most]{tcolorbox}

\newtcolorbox{note}[1][]{%
  enhanced jigsaw, % better frame drawing
  borderline west={2pt}{0pt}{red}, % straight vertical line at the left edge
  sharp corners, % No rounded corners
  boxrule=0pt, % no real frame,
  fonttitle={\large\bfseries},
  coltitle={black},  % Black colour for title
  title={Note:\ },  % Fixed title
  attach title to upper, % Move the title into the box
  #1
}

\usepackage{blindtext}

\begin{document}

\blindtext
\begin{note}
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}

\begin{note}[borderline west={2pt}{0pt}{blue}, colback=yellow, drop shadow={red,opacity=0.6}]
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}

\blindtext

\end{document}

在此处输入图片描述

有关等的更多复杂用法,borderline请参阅用彩色垂直半线重现 PDF 表格

这是一个版本,其中note框的第一行与枚举环境的标签对齐:

\documentclass[11pt]{scrartcl}

\usepackage{enumitem}
\usepackage[most]{tcolorbox}


\makeatletter
\newtcolorbox{note}[1][]{%
  enhanced jigsaw, % better frame drawing
  borderline west={1pt}{0pt}{black}, % straigh vertical line at the left edge
  sharp corners, % No rounded corners
  boxrule=0pt, % no real frame,
  fonttitle={\large\bfseries},
  coltitle={black},  % Black colour for title
  title={Note:\ },  % Fixed title
  attach title to upper, % Move the title into the box,
  right=0pt,
  top=0pt,
  bottom=0pt,
  frame hidden,
  baseline={\tcb@height-2\kvtcb@boxsep+\baselineskip-2\lineskip}, %%%% Needs more to be done for other setups
  #1
}
\makeatother

\usepackage{blindtext}

\begin{document}

\blindtext
\begin{note}
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}

\begin{enumerate}
\item \begin{note}
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}

\item \begin{note}
    Here comes a useful note for demonstration. Let's add enough text to break the line, just to be sure the vertical line extends with the text.
\end{note}

\end{enumerate}



\blindtext

\end{document}

在此处输入图片描述

相关内容