我使用tikz
和tcolorbox
来创建定理的框。我计算标题的长度以切割框架。一切正常,直到计数器达到 10 或 100,长度计算出错或其他问题,结果如下:
一开始我们可能认为它必须在计数器达到 10、20、30……等时出现,但它只在 10 和 100 时才会出现。它也可能在 1000 时出现。
梅威瑟:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[most]{tcolorbox}
\usepackage{bbm}
\usetikzlibrary{calc}
\newlength{\longueur}
\definecolor{vert}{RGB}{51,92,41}
\definecolor{bleu}{RGB}{64,106,152}
\definecolor{rouge}{RGB}{178,27,15}
\newcommand{\definebox}[4]{
\ifstrequal{#4}{o}{
\newcounter{#1}
\newenvironment{#1}[1][]{
\settowidth{\longueur}{\textbf{\textsc{#3} \arabic{#1}##1}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\refstepcounter{#1}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1}##1}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1}##1}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
{\newenvironment{#1}[1][]{
\settowidth{\longueur}{\textbf{\textsc{#3}##1}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3}##1}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3}##1}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
}
\definebox{bv}{vert}{Définition}{o}
\definebox{bb}{bleu}{Proposition}{o}
\definebox{br}{rouge}{Théorème}{o}
\begin{document}
\begin{bb}[ -- Théorème d'inversion locale]
Si $f:U \to F$ est $C^{1}$ avec $a \in U$ tel que $df(a) \in \textrm{Isom}(E,F)$, alors il existe un voisinage ouvert $V \subset U$ de $a$ et un voisinage ouvert $W$ de $f(a)$ tel que $f_{|V}:V \to W$ est un $C^{1}$-difféomorphisme.
\end{bb}
\end{document}
Jinwen 的新 MWE:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[most]{tcolorbox}
\usepackage{tipa}
\usetikzlibrary{calc}
\newlength{\longueur}
\definecolor{vert}{RGB}{51,92,41}
\definecolor{bleu}{RGB}{64,106,152}
\definecolor{rouge}{RGB}{178,27,15}
\newcommand{\definebox}[4]{
\ifstrequal{#4}{o}{
\newcounter{#1}
\newenvironment{#1}[1][]{
\refstepcounter{#1}
\def\temp{##1}
\ifx\temp\empty
\def\tempdescription{\!\!}
\else
\def\tempdescription{-- ##1}
\fi
\settowidth{\longueur}{\textbf{\textsc{#3} \arabic{#1} \tempdescription}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
{\newenvironment{#1}[1][]{
\settowidth{\longueur}{\textbf{\textsc{#3} \tempdescription}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
}
\definebox{bv}{vert}{Définition}{o}
\definebox{bb}{bleu}{Proposition}{o}
\definebox{br}{rouge}{Théorème}{o}
\begin{document}
\begin{bb}
test
\end{bb}
\end{document}
答案1
由于您已将其放在\refstepcounter
之后\settowidth
,当计数器值为 10、100、1000 等时,测量的宽度为 9、99、999 等。因此,要纠正此行为,您需要将其放在\refstepcounter
开头。
另外,我已将--
定理描述中的部分移至 tcolorbox 设置,因此您无需自己明确编写它们。(代码将测试是否给出了描述,并且当描述不为空时--
将添加 - 测试使用传统\ifx
方法,因此它并不总是可靠的,例如当您的描述包含条件时\if...
,但它应该足以满足您的目的。)
\documentclass{article}
\usepackage[most]{tcolorbox}
\usetikzlibrary{calc}
\newlength{\longueur}
\definecolor{vert}{RGB}{51,92,41}
\definecolor{bleu}{RGB}{64,106,152}
\definecolor{rouge}{RGB}{178,27,15}
\newcommand{\definebox}[4]{
\ifstrequal{#4}{o}{
\newcounter{#1}
\newenvironment{#1}[1][]{
\refstepcounter{#1}
\def\temp{##1}
\ifx\temp\empty
\def\tempdescription{\hspace{-1ex}}
\else
\def\tempdescription{-- ##1}
\fi
\settowidth{\longueur}{\textbf{\textsc{#3} \arabic{#1} \tempdescription}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \arabic{#1} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
{\newenvironment{#1}[1][]{
\settowidth{\longueur}{\textbf{\textsc{#3} \tempdescription}}
\pgfmathsetmacro{\len}{\the\longueur*1pt/1cm}
\begin{tcolorbox}[enhanced,opacityback=0,opacityframe=0,before skip=0.5cm,left=2.5mm,right=2.5mm,top=3mm,bottom=0.5mm,breakable,overlay unbroken={
\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay first={\node[anchor=west,#2] at ($(interior.north west)+(0.345,0.05)$){\textbf{\textsc{#3} \tempdescription}\vphantom{/Î)}};
\draw[very thick,#2]($(interior.north west)+(0.362,0.025)$)--($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$)--($(interior.north west)+(0.362,0.025)+(\len,0)+(0.215,0)$);},overlay middle={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$); \draw[very thick,#2]($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},overlay last={
\draw[very thick,#2]($(interior.north west)+(-0.025,0.025)$)--($(interior.south west)+(-0.025,-0.025)$)--($(interior.south east)+(+0.025,-0.025)$)--($(interior.north east)+(+0.025,+0.025)$);},after={\vspace{0.2ex} \noindent}]}
{\end{tcolorbox}}}
}
\definebox{bv}{vert}{Définition}{o}
\definebox{bb}{bleu}{Proposition}{o}
\definebox{br}{rouge}{Théorème}{o}
\begin{document}
\setcounter{bb}{99}
\begin{bb}[Théorème d'inversion locale]
Si $f:U \to F$ est $C^{1}$ avec $a \in U$ tel que $df(a) \in \textrm{Isom}(E,F)$, alors il existe un voisinage ouvert $V \subset U$ de $a$ et un voisinage ouvert $W$ de $f(a)$ tel que $f_{|V}:V \to W$ est un $C^{1}$-difféomorphisme.
\end{bb}
\begin{bb}
Si $f:U \to F$ est $C^{1}$ avec $a \in U$ tel que $df(a) \in \textrm{Isom}(E,F)$, alors il existe un voisinage ouvert $V \subset U$ de $a$ et un voisinage ouvert $W$ de $f(a)$ tel que $f_{|V}:V \to W$ est un $C^{1}$-difféomorphisme.
\end{bb}
\end{document}
答案2
尽管@Jinwen 是问题的正确答案,但有一个建议可以简化问题:为什么不直接使用白色填充的独立标题来表示工作,这样就不需要计算任何东西了?
概念验证示例(主要来自手册,提示从这里):
\documentclass{article}
\usepackage{tcolorbox}
\tcbuselibrary{many}
\newtcolorbox{mybox}[2][]{colback=white,
colframe=cyan!50!black,fonttitle=\bfseries,
colbacktitle=white,enhanced, coltitle=blue,
attach boxed title to top left={xshift=0.5cm,
yshift=-\tcboxedtitleheight/2},
boxed title style={colframe=white},
title={#2},#1}
\begin{document}
\begin{mybox}[]{Hello there}
This is my own box with a mandatory title
and options.
\end{mybox}
\end{document}