tcolorbox的编号问题?

发布于 2024-03-17 10:14:22

我采用tcolorbox自定义了一个盒子,想要得到一个有可选参数时没有编号,没有可选参数时有编号且编号只有在没有可选参数输入时才增加,但是目前得到的结果会自己增加,请问大佬们该怎么修改呢?MWE如下:

\documentclass{article}

\usepackage{tcolorbox}

\tcbuselibrary{skins,minted}

%% 重定义minted环境前的行号样式
\renewcommand{\theFancyVerbLine}
{%
  \ttfamily\textcolor[rgb]{0.5,0.5,1.0}
  {%
    \footnotesize\arabic{FancyVerbLine}
  }
}

\newcounter{myexample}
\NewTCBListing[use counter=myexample]{myjava}{!O{code:\thetcbcounter}}{%
    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,
    title = #1,
    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]
    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}

\end{document}

image.png

相当于想让这里的code:2显示为code:1,后续依次类推

查看更多

关注者
0
被浏览
1.1k
3 个回答
雾月
雾月 2024-03-25
这家伙很懒,什么也没写!

如果没有递增计数器,\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 之前使用。

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览