【发布时间】:2020-10-06 18:02:05
【问题描述】:
我为我的数学笔记创建了一个示例环境。它将示例的标题作为输入,并使用 tikz 绘制一些线条。但是,这样做需要标题的长度。
当使用\newlength{\lengthname} 后跟\settowidth{\lengthname}{[length]} 只调用一次环境时,这相对容易。但是,一旦多次调用它,就必须定义不同的长度。我(诚然很差)的解决方法是在每次使用示例环境时传递不同长度的名称#2。
如何在每次使用我的环境时创建一个独特的\newlength{\unique},或者,有没有更好的方法来实现我的目标?
\newenvironment{example}[2] % Example Environment
{\refstepcounter{example}
\newlength{#2}
\settowidth{#2}{\small \textbf{Example \thesection.\theexample} --- #1}
\bigskip\begin{tikzpicture}
\draw (-0.5\columnwidth,-0.2)--(-0.5\columnwidth,0)--(0.5\columnwidth,0)--(0.5\columnwidth,-0.2);
\fill[white] (-0.5#2-5pt,-1pt) rectangle (0.5#2+5pt,1pt);
\tikzlabel{0}{-0.4}{\text{\small \textbf{Example \thesection.\theexample} --- #1}}
\end{tikzpicture}}
%
{\begin{tikzpicture}
\draw (-0.5\columnwidth,0.2) -- (-0.5\columnwidth,0) -- (0.5\columnwidth,0) -- (0.5\columnwidth,0.2);
\end{tikzpicture}}
非常感谢。
【问题讨论】:
-
你能不能做一个minimal reproducible example 而不仅仅是一个代码片段?
-
为什么每次调用环境都使用新的长度?为什么不能简单地重复使用同一个?
-
无论如何,如果 tcolorbox 有一个定理库,为什么还要重新发明轮子?