我会尽量具体一点。我的情况是这样的:
*图片上应该写着“(...) 我需要的是能够 (...)”
尽管我在图片中解释了我需要什么,但这个问题的完美答案是让命令 \teo 决定哪种情况:有定理名称还是没有定理名称。我的一些代码如下:
\documentclass[11pt, oneside]{book}
% Commands
\newcommand{\teo}[1]{{\bfseries\upshape{\hyperref[#1]{\nameref{#1} (Teorema \ref{#1})}}}}
% Environment I've used
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}
\newcounter{theorem}[chapter]
\renewcommand{\thetheorem}{\arabic{chapter}.\arabic{theorem}}
\makeatletter
\newenvironment{theorem}[1][]{%
\refstepcounter{theorem}
\protected@edef\@currentlabelname{#1}
\protected@edef\@currentlabel{#1}
\tcbset{enhanced, breakable, colbacktitle = gray!50, coltitle = black, fonttitle = \bfseries,
attach boxed title to top left = {yshift = -\tcboxedtitleheight/2, xshift = 0.55cm},
boxed title style = {rounded corners, colframe = gray!50, boxsep = 4pt},
top = \tcboxedtitleheight/2+2mm,
opacityframe = 0, opacityback = 0,
borderline horizontal = {0.5mm}{0pt}{gray!50}}
\ifstrempty{#1}%
{\tcbset{title = Teorema \thetheorem}}%
{\tcbset{title = #1 (Teorema \thetheorem)}}
\begin{tcolorbox}[]\relax}{%
\end{tcolorbox}
}
\makeatother
\begin{document}
\begin{theorem}[Theorem's Name] \label{t1}
some text
\end{theorem}
\begin{theorem} \label{t2}
This theorem has no name.
\end{theorem}
If I refer with \verb=\teo{t1}=, \teo{t1}, it calls the all theorem (with its name and number) and want I want is to be able to call \teo{t2} with the same command (\verb=\teo{t2}=) but without the extra-space and the parenthesis.
\end{document}
我希望我已经解释清楚了。谢谢。
答案1
您可以进入更深的层次;该名称存储在由 扩展而来的第三个括号组中\r@<label>
。
我们可以检查它是否为空,并采取相应措施。
我使用\nameref*
和\ref*
不来创建多个链接,而只使用由生成的链接\hyperref
。
\documentclass[11pt, oneside]{book}
% Packages I use
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}
\usepackage{hyperref}
\newcounter{theorem}[chapter]
\renewcommand{\thetheorem}{\arabic{chapter}.\arabic{theorem}}
\makeatletter
\newenvironment{theorem}[1][]{%
\refstepcounter{theorem}%
\protected@edef\@currentlabelname{#1}%
\tcbset{enhanced, breakable, colbacktitle = gray!50, coltitle = black, fonttitle = \bfseries,
attach boxed title to top left = {yshift = -\tcboxedtitleheight/2, xshift = 0.55cm},
boxed title style = {rounded corners, colframe = gray!50, boxsep = 4pt},
top = \tcboxedtitleheight/2+2mm,
opacityframe = 0, opacityback = 0,
borderline horizontal = {0.5mm}{0pt}{gray!50}}
\ifstrempty{#1}%
{\tcbset{title = Teorema \thetheorem}}%
{\tcbset{title = #1 (Teorema \thetheorem)}}
\begin{tcolorbox}[]\relax}{%
\end{tcolorbox}
}
\newcommand{\teo}[1]{{%
\normalfont\bfseries\upshape
\@ifundefined{r@#1}{%
% ref not yet defined
Teorema \ref{#1}%
}{%
\protected@edef\@tempa{\expandafter\expandafter\expandafter\@thirdoffive\csname r@#1\endcsname}%
\ifx\@tempa\@empty
% no name
\hyperref[#1]{Teorema~\ref*{#1}}%
\else
% theorem has name
\hyperref[#1]{\nameref*{#1} (Teorema~\ref*{#1})}%
\fi
}%
}}
\makeatother
\begin{document}
\begin{theorem}[Theorem's Name] \label{t1}
some text
\end{theorem}
\begin{theorem} \label{t2}
This theorem has no name.
\end{theorem}
If I refer with \verb=\teo{t1}=, \teo{t1},
it calls the all theorem (with its name and number)
and want I want is to be able to call \teo{t2} with
the same command (\verb=\teo{t2}=) but without the
extra-space and the parenthesis.
\end{document}
更好的实现
\documentclass[11pt, oneside]{book}
% Packages I use
\usepackage{tcolorbox}
\tcbuselibrary{skins, breakable}
\usepackage{hyperref}
\newcounter{theorem}[chapter]
\renewcommand{\thetheorem}{\arabic{chapter}.\arabic{theorem}}
\makeatletter
\newenvironment{theorem}[1][]{%
\refstepcounter{theorem}%
\protected@edef\@currentlabelname{#1}%
\tcbset{enhanced, breakable, colbacktitle = gray!50, coltitle = black, fonttitle = \bfseries,
attach boxed title to top left = {yshift = -\tcboxedtitleheight/2, xshift = 0.55cm},
boxed title style = {rounded corners, colframe = gray!50, boxsep = 4pt},
top = \tcboxedtitleheight/2+2mm,
opacityframe = 0, opacityback = 0,
borderline horizontal = {0.5mm}{0pt}{gray!50}}
\ifstrempty{#1}%
{\tcbset{title = Teorema \thetheorem}}%
{\tcbset{title = #1 (Teorema \thetheorem)}}
\begin{tcolorbox}[]\relax}{%
\end{tcolorbox}
}
\makeatother
% Commands
\NewDocumentCommand{\teoformat}{m}{\textnormal{\textbf{#1}}}
\ExplSyntaxOn
\NewDocumentCommand{\teo}{m}
{
\tl_if_exist:cTF { r@#1 }
{% the reference is defined
\peluche_teo:cn { r@#1 } { #1 }
}
{% the reference is not yet defined
\teoformat{Teorema\nobreakspace\ref{#1}}%
}
}
\cs_new_protected:Nn \peluche_teo:Nn
{% the reference is defined
\exp_args:Ne \tl_if_empty:nTF { \tl_item:Nn #1 { 3 } }
{% no name
\hyperref[#2]{\teoformat{Teorema\nobreakspace\ref*{#2}}}
}
{% name exists
\hyperref[#2]{\teoformat{\nameref*{#2}~(Teorema\nobreakspace\ref*{#2})}}
}
}
\cs_generate_variant:Nn \peluche_teo:Nn { c }
\ExplSyntaxOff
\begin{document}
\begin{theorem}[Theorem's Name] \label{t1}
some text
\end{theorem}
\begin{theorem} \label{t2}
This theorem has no name.
\end{theorem}
If I refer with \verb=\teo{t1}=, \teo{t1},
it calls the all theorem (with its name and number)
and want I want is to be able to call \teo{t2} with
the same command (\verb=\teo{t2}=) but without the
extra-space and the parenthesis.
\end{document}
这个想法与以前相同,但更容易从中提取第三项,\r@<label>
其行为类似于标记列表变量,以便稳健地测试它是否为空并采取适当的操作。
我这样介绍\teoformat
,这样就可以更轻松地更改参考文献的格式,而不会因许多格式化指令而污染代码。