如果没有递增计数器,\ref
显示为空,hyperref
不会为空的文本创建超链接。
只能使用 \hyperref[<label>]{<text>}
或 \pageref
。
也可以在写入 \label
时就指定需要显示的文字:
\documentclass{article}
\usepackage{tcolorbox}
\usepackage{hyperref}
\tcbuselibrary{skins,minted}
%% 重定义minted环境前的行号样式
\renewcommand{\theFancyVerbLine}
{%
\ttfamily\textcolor[rgb]{0.5,0.5,1.0}
{%
\footnotesize\arabic{FancyVerbLine}
}
}
\makeatletter
\protected\def\setnextlabeltext#1{\AddToHookNext{label}{\protected@edef\@currentlabel{#1}%
\edef\@currentlabel{\detokenize\expandafter{\@currentlabel}}}}
\makeatother
\newcounter{myexample}
\NewTCBListing{myjava}{O{} O{}}{%
listing engine = minted,
minted style = colorful,
minted language = java,
%minted label =#1
minted options={%
fontsize = \small,
breaklines,
autogobble,
linenos,
numbersep = -1.5mm,
%numbersep = 3pt,
keywordcase = lower, % 关键字小写, upper为大写, capitalize为首字母大写, 默认为lower
mathescape = true, % 在注释中可以书写并渲染数学公式
showspaces = false,
tabsize = 4,
texcomments = false,
%framerule = 1pt,
%breaklines = true,
fontfamily = tt,
},
%colback = blue!5!white,
%colframe = blue!75!black,
colback = white,
colframe = black,
colbacktitle=white,
coltitle=black,
listing only,
left = 5mm,
enhanced,
overlay = {\begin{tcbclipinterior}\fill[red!20!blue!20!white] (frame.south west)
rectangle ([xshift = 5mm]frame.north west);\end{tcbclipinterior}},
attach boxed title to top center = {yshift =-3mm},
center title,
%IfValueTF = {#1}{title=#1}{step=myexample, title=code:\themyexample},
IfEmptyTF = {#1}{% 第一个没有值
step=myexample, title=code:\themyexample,
IfEmptyTF={#2}{label=code:\themyexample}{label={#2}}
}{% 第一个有值
title={#1}, IfEmptyTF={#2}{}{phantom=\setnextlabeltext{#1},phantomlabel={#2}}
},
fonttitle = \ttfamily\large,
boxrule=0.75pt,
boxed title style={
boxrule=0pt,
colframe=white,
size = fbox,
},
%frame hidden
%boxed title style={arc=0pt,outer arc=0pt,boxrule=0pt},
}
\begin{document}
\begin{myjava}[test][aaa]
public class HelloWorld {
// A `Hello World' in Java
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
\end{myjava}
\begin{myjava}
public class HelloWorld {
// A `Hello World' in Java
public static void main(String[] args) {
System.out.println("Hello World!");
}
}
\end{myjava}
a\hyperref[aaa]{text}.\pageref{aaa}.\ref{aaa}.\ref{code:1}.
\end{document}
这个 \setnextlabeltext
也可以在递增计数器之后,\label
之前使用。
问 tcolorbox的编号问题?