我有一个与这个问题相关的问题:tcolorbox 中的精确尺寸。
我产生了几种环境,“定义”、“命题”等,并且我想将我的工作统一到几种环境情况下,即制作一个通用框,适用于所有类型的环境(这里,以两种环境、定义和命题作为我的例子)。
我创建了一个环境“mybox”,尝试在其中进行这项工作。我可以使用命令将规则的颜色编码为该环境的变量,Col#2
#2 可以是定义或命题,但节点\#2node
(对于这些示例,必须是\propositionnode
和\definitionnode
)无法识别,如在编译中看到的。我该如何修复这个问题?
根据引用的原始 .tex 文件的代码项目如下:
\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{polyglossia}
\setmainlanguage{english}
\colorlet{Colproposition}{blue}
\colorlet{Coldefinition}{green}
\newcounter{definition}[chapter]
\newcounter{proposition}[chapter]
\renewcommand{\thedefinition}{\arabic{definition}}
\newcommand{\definitionlabel}{\textbf{Def \thedefinition}}
\newcommand{\definitionnode}{%
\node[fill=Coldefinition!5!white, draw=Coldefinition!75!black, line width=.5pt, below right, minimum height=2em,
text width=.8cm, align=center] at ([xshift=3mm,yshift=-1mm]frame.north east) {\definitionlabel}}
\renewcommand{\theproposition}{\arabic{proposition}}
\newcommand{\propositionlabel}{\textbf{Prop \theproposition}}
\newcommand{\propositionnode}{%
\node[fill=Colproposition!5!white, draw=Colproposition!75!black, line width=.5pt, below right, minimum height=2em,
text width=.8cm, align=center] at ([xshift=3mm,yshift=-1mm]frame.north east) {\propositionlabel}}
\newenvironment{definition}[1][]{%
\refstepcounter{definition}%
\begin{tcolorbox}[enhanced jigsaw,breakable,colback=white,colframe=white,arc=0mm,rightrule=0mm,leftrule=0mm,toprule=0mm,
bottomrule=0mm,top=2mm,bottom=2mm, left=2mm, right=2mm,extrude right by=-15mm,
before upper={\parshape 4
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \hsize},
underlay unbroken and first={%
\definitionnode;
\draw [Coldefinition!75!black,line width=.5mm]([xshift=15mm,yshift=.25mm]frame.north east)--+(-\textwidth/4,0);
\draw [Coldefinition!75!black,line width=.5mm]([xshift=14.75mm,yshift=.25mm]frame.north east)--+(0,-10mm);
\draw [Coldefinition!75!black,line width=.5mm]([yshift=.25mm]frame.north west)--+(\textwidth/4,0);
\draw [Coldefinition!75!black,line width=.5mm]([xshift=.25mm,yshift=.25mm]frame.north west)--+(0,-10mm);
},
underlay unbroken and last={%
\draw [Coldefinition!75!black,line width=.5mm]([yshift=.25mm]frame.south west)(\textwidth/4,0)--(0,0);
\draw [Coldefinition!75!black,line width=.5mm]([xshift=.25mm,yshift=.25mm]frame.south west)--+(0,10mm);
\draw [Coldefinition!75!black,line width=.5mm]([xshift=14.75mm,yshift=.25mm]frame.south east)--+(0,10mm);
\draw [Coldefinition!75!black,line width=.5mm]([yshift=.25mm]frame.south east)(3*\textwidth/4,0)--(\textwidth,0);
}
]}{%
\end{tcolorbox}}
\newenvironment{proposition}[1][]{%
\refstepcounter{proposition}%
\begin{tcolorbox}[enhanced jigsaw,breakable,colback=white,colframe=white,arc=0mm,rightrule=0mm,leftrule=0mm,toprule=0mm,
bottomrule=0mm,top=2mm,bottom=2mm, left=2mm, right=2mm,
before upper={\parshape 4
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \hsize},
extrude right by=-15mm,
underlay unbroken and first={%
\propositionnode;
\draw [Colproposition!75!black,line width=.5mm]([xshift=15mm,yshift=.25mm]frame.north east)--+(-\textwidth/4,0);
\draw [Colproposition!75!black,line width=.5mm]([xshift=14.75mm,yshift=.25mm]frame.north east)--+(0,-10mm);
\draw [Colproposition!75!black,line width=.5mm]([yshift=.25mm]frame.north west)--+(\textwidth/4,0);
\draw [Colproposition!75!black,line width=.5mm]([xshift=.25mm,yshift=.25mm]frame.north west)--+(0,-10mm);
},
underlay unbroken and last={%
\draw [Colproposition!75!black,line width=.5mm]([yshift=.25mm]frame.south west)(\textwidth/4,0)--(0,0);
\draw [Colproposition!75!black,line width=.5mm]([xshift=.25mm,yshift=.25mm]frame.south west)--+(0,10mm);
\draw [Colproposition!75!black,line width=.5mm]([xshift=14.75mm,yshift=.25mm]frame.south east)--+(0,10mm);
\draw [Colproposition!75!black,line width=.5mm]([yshift=.25mm]frame.south east)(3*\textwidth/4,0)--(\textwidth,0);
}
]}{%
\end{tcolorbox}}
\newenvironment{mybox}[2][]{%
\refstepcounter{#2}%
\begin{tcolorbox}[enhanced jigsaw,breakable,colback=white,colframe=white,arc=0mm,rightrule=0mm,leftrule=0mm,toprule=0mm,
bottomrule=0mm,top=2mm,bottom=2mm, left=2mm, right=2mm,extrude right by=-15mm,
before upper={\parshape 4
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \hsize},
underlay unbroken and first={%
\#2node;
\draw [Col#2!75!black,line width=.5mm]([xshift=15mm,yshift=.25mm]frame.north east)--+(-\textwidth/4,0);
\draw [Col#2!75!black,line width=.5mm]([xshift=14.75mm,yshift=.25mm]frame.north east)--+(0,-10mm);
\draw [Col#2!75!black,line width=.5mm]([yshift=.25mm]frame.north west)--+(\textwidth/4,0);
\draw [Col#2!75!black,line width=.5mm]([xshift=.25mm,yshift=.25mm]frame.north west)--+(0,-10mm);
},
underlay unbroken and last={%
\draw [Col#2!75!black,line width=.5mm]([yshift=.25mm]frame.south west)(\textwidth/4,0)--(0,0);
\draw [Col#2!75!black,line width=.5mm]([xshift=.25mm,yshift=.25mm]frame.south west)--+(0,10mm);
\draw [Col#2!75!black,line width=.5mm]([xshift=14.75mm,yshift=.25mm]frame.south east)--+(0,10mm);
\draw [Col#2!75!black,line width=.5mm]([yshift=.25mm]frame.south east)(3*\textwidth/4,0)--(\textwidth,0);
}
]}{%
\end{tcolorbox}}
\begin{document}
\pagestyle{empty}
\begin{definition}
A definition produced by the 'definition' environment
\vspace{1cm}
\end{definition}
\begin{proposition}
A proposition produced by the 'proposition' environment
\vspace{1cm}
\end{proposition}
\begin{mybox}{proposition}
A proposition produced by the 'mybox' environment
\vspace{1cm}
\end{mybox}
\begin{mybox}{definition}
A definition produced by the 'mybox' environment
\vspace{1cm}
\end{mybox}
\end{document}
编译后得到如下结果,我们可以看到标题节点没有出现在框中:
答案1
一个可能的解决方案是定义几个styles
将在tcolorbox
定义内部使用的。
这里mystyle
定义了一个具有两个参数的通用框,第一个参数是颜色,第二个参数是右上角框中使用的标签。
右上角的框由\newcommand
两个参数定义:标签和颜色。
标题定义了另一种样式。此定义不考虑在https://tex.stackexchange.com/a/395963/1952。
再次,就像https://tex.stackexchange.com/a/396010/1952最初提议的环境已被取代\newtcolorboxes
\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{varwidth}
\tcbuselibrary{skins,breakable}
\usepackage{polyglossia}
\setmainlanguage{english}
\newfontfamily{\englishfont}{Arial}
\newcommand{\myboxnode}[2]{%
\node[fill=#2!5!white, draw=#2!75!black, line width=.5pt,
below right, minimum height=2em,
text width=.8cm, align=center,
font=\bfseries] at ([xshift=3mm,yshift=-1mm]frame.north east) {#1 \thetcbcounter}}
\tcbset{
mystyle/.style 2 args={
enhanced jigsaw, breakable,
colback=white, colframe=white,
sharp corners,
boxrule = 0mm,
top=2mm, bottom=2mm, left=2mm, right=2mm,
varwidth boxed title,
attach boxed title to top right={%
xshift=.5mm,yshift=- \tcboxedtitleheight+.5mm},
boxed title style={%
enhanced,
sharp corners,
colframe=#1!75!black,
right=1mm,
boxrule=.5mm},
colbacktitle=#1!5!white,
coltitle=black,
extrude right by=-15mm,
underlay unbroken and first={%
\myboxnode{#2}{#1};
\draw [#1!75!black,line width=.5mm]([xshift=14.75mm,yshift=.25mm]frame.north east)--+(0,-15mm);
\draw [#1!75!black,line width=.5mm]([xshift=15mm,yshift=.25mm]frame.north east)--+(-\textwidth/4,0);
\draw [#1!75!black,line width=.5mm]([yshift=.25mm]frame.north west)--+(\textwidth/4,0);
\draw [#1!75!black,line width=.5mm]([xshift=.25mm,yshift=.25mm]frame.north west)--+(0,-15mm);
},
underlay unbroken and last={%
\draw [#1!75!black,line width=.5mm]([yshift=.25mm]frame.south west)(\textwidth/4,0)--(0,0);
\draw [#1!75!black,line width=.5mm]([xshift=.25mm,yshift=.25mm]frame.south west)--+(0,15mm);
\draw [#1!75!black,line width=.5mm]([xshift=14.75mm,yshift=.25mm]frame.south east)--+(0,15mm);
\draw [#1!75!black,line width=.5mm]([yshift=.25mm]frame.south east)(3*\textwidth/4,0)--(\textwidth,0);
},
},
mytitle/.style={
code={%
\ifstrempty{#1}
{\tcbset{
before upper={\parshape 4
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \hsize}}}
{\tcbset{
title={#1},
before upper={\vspace*{\baselineskip}\parshape 3
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \hsize}}}
},
}
}
\newtcolorbox[auto counter]{proposition}[2][]{%
mystyle={blue}{Prop},
mytitle={#2},
#1}
\newtcolorbox[auto counter]{definition}[2][]{%
mystyle={green}{Def},
mytitle={#2},
#1}
\begin{document}
\pagestyle{empty}
\begin{definition}{}
A non-titled definition
\vspace{2.2cm}
\end{definition}
\begin{proposition}{A long title}
A titled proposition
\vspace{2cm}
\end{proposition}
\begin{definition}[height=2cm]{Another long title}
A titled definition
\end{definition}
\end{document}
更新
我已将varwidth boxed node
其改为tikznode boxed node
。这样就可以修复minimum width
以解决短标题问题。
解决了右下角线的问题,绘制了角落default
,如果有标题框,则再次绘制下线直到标题结束。
\documentclass{book}
\usepackage[most]{tcolorbox}
\usepackage{varwidth}
\tcbuselibrary{skins,breakable}
\usepackage{polyglossia}
\setmainlanguage{english}
\newfontfamily{\englishfont}{Arial}
\newcommand{\myboxnode}[2]{%
\node[fill=#2!5!white, draw=#2!75!black, line width=.5pt,
below right, minimum height=2em,
text width=.8cm, align=center,
font=\bfseries] at ([xshift=3mm,yshift=-1mm]frame.north east) {#1 \thetcbcounter}}
\tcbset{
mystyle/.style 2 args={
enhanced jigsaw, breakable,
colback=white, colframe=white,
sharp corners,
boxrule = 0mm,
top=2mm, bottom=2mm, left=2mm, right=2mm,
% varwidth boxed title,
tikznode boxed title={minimum width=\textwidth/4-15mm-4.5mm},
attach boxed title to top right={%
xshift=.5mm,yshift=- \tcboxedtitleheight+.5mm},
boxed title style={%
enhanced,
sharp corners,
colframe=#1!75!black,
left=1mm, right=1mm,
boxrule=.5mm},
colbacktitle=#1!5!white,
coltitle=black,
extrude right by=-15mm,
underlay unbroken and first={%
\myboxnode{#2}{#1};
\draw [#1!75!black,line width=.5mm]([shift={(15mm-.5\pgflinewidth,-15mm+.5\pgflinewidth)}]frame.north east)|-([shift={(-\textwidth/4+15mm,+.5\pgflinewidth)}]frame.north east);
\draw [#1!75!black,line width=.5mm]([shift={(.5\pgflinewidth,-15mm+.5\pgflinewidth)}]frame.north west)|-([shift={(\textwidth/4,+.5\pgflinewidth)}]frame.north west); },
underlay unbroken and last={%
\draw [#1!75!black,line width=.5mm]([yshift=.25mm]frame.south west)(\textwidth/4,0)--(0,0);
\draw [#1!75!black,line width=.5mm]([xshift=.25mm,yshift=.25mm]frame.south west)--+(0,15mm);
\draw [#1!75!black,line width=.5mm]([xshift=14.75mm,yshift=.25mm]frame.south east)--+(0,15mm);
\draw [#1!75!black,line width=.5mm]([yshift=.25mm]frame.south east)(3*\textwidth/4,0)--(\textwidth,0);
},
},
mytitle/.style 2 args={
code={%
\ifstrempty{#1}
{\tcbset{
before upper={\parshape 4
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \hsize}}}
{\tcbset{
title={#1},
before upper={\vspace*{\baselineskip}\parshape 3
0pt \dimexpr \hsize-14mm\relax
0pt \dimexpr \hsize-14mm\relax
0pt \hsize},
underlay unbroken and last={
\draw [#2!75!black,line width=.5mm](frame.south east)--++(180:\tcboxedtitlewidth-\pgflinewidth);
}}}
},
}
}
\newtcolorbox[auto counter]{proposition}[2][]{%
mystyle={blue}{Prop},
mytitle={#2}{blue},
#1}
\newtcolorbox[auto counter]{definition}[2][]{%
mystyle={green}{Def},
mytitle={#2}{green},
#1}
\begin{document}
\pagestyle{empty}
\begin{definition}{}
A non-titled definition
\vspace{2.2cm}
\end{definition}
\begin{proposition}{A long title}
A titled proposition
\vspace{2cm}
\end{proposition}
\begin{definition}[height=2cm]{A}
A titled definition
\end{definition}
\end{document}