tcolorbox 内嵌套铸造列表的问题

tcolorbox 内嵌套铸造列表的问题

我想在 a 的下部插入一些 JAVA 代码tcolorbox(见图)。当我简单地使用 a 时,tcolorbox它可以工作,但使用\DeclareTotalTColorBoxa 则不行。以下 MEW 提供了更多详细信息。

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fullpage}
\usepackage[all]{tcolorbox}

% Command that should accept a tcblisting as a second mandatory. It leads to a compilation error when used.
\DeclareTotalTColorBox{\javaapp}{ O{red} m m}{enhanced,colframe=#1!75!black,colback=#1!5!white,title=JAVA application}
 {
  #2
  \tcblower
  #3
 }

% Command that should accept a JAVA code as a second mandatory. It leads to a compilation error when used.
\DeclareTotalTColorBox{\javaappwithminted}{ O{red} m m }{enhanced,colframe=#1!75!black,colback=#1!5!white,title=JAVA application}
 {
  #2
  \tcblower
  \begin{tcblisting}{listing engine=minted,minted style=trac,minted language=java,
   colback=red!5!white,colframe=red!75!black,listing only,enhanced,nobeforeafter,skin=enhancedmiddle jigsaw,interior hidden,leftrule=0mm,rightrule=0mm,boxsep=0mm,top=0mm,bottom=0mm}
   #3
  \end{tcblisting}
 }

\begin{document}

\begin{tcolorbox}[enhanced,colframe=red!75!black,colback=red!5!white,title=JAVA application]
  Write a 'Hello Word' java class
 \tcblower
  \begin{tcblisting}{listing engine=minted,minted style=trac,minted language=java,
     colback=red!5!white,colframe=red!75!black,listing only,enhanced,nobeforeafter,skin=enhancedmiddle jigsaw,interior hidden,leftrule=0mm,rightrule=0mm,boxsep=0mm,top=0mm,bottom=0mm}
public class HelloWorld {
    // A ’Hello World’ in Java
    public static void main(String[] args) {
        System.out.println("Hello World!");
    }
}
\end{tcblisting}
\end{tcolorbox}

\end{document}

在此处输入图片描述

答案1

问题是逐字内容不能直接嵌套到其他内容中。

的结尾tcblisting无法正确识别,并生成错误——TeX必须查看“字符串” \endtcblisting,这可以通过使用重新扫描内容来实现\scantokens

为了提供良好的输出,请使用+v参数说明符(+ 表示多行,\par以及v表示逐字),而不是m+m

\DeclareTotalTColorBox{\javaappwithminted}{ O{red}+m+v }{enhanced,colframe=#1!75!black,colback=#1!5!white,title=JAVA application}
 {
  #2
  \tcblower
  \scantokens{%
  \begin{tcblisting}{listing engine=minted,minted style=trac,minted language=java,
      colback=red!5!white,colframe=red!75!black,listing only,enhanced,nobeforeafter,skin=enhancedmiddle jigsaw,interior hidden,leftrule=0mm,rightrule=0mm,boxsep=0mm,top=0mm,bottom=0mm}
    #3
    \end{tcblisting}
  }
}

完整代码如下:

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fullpage}
\usepackage[all]{tcolorbox}

% Command that should accept a tcblisting as a second mandatory. It leads to a compilation error when used.
\DeclareTotalTColorBox{\javaapp}{ O{red} m m}{enhanced,colframe=#1!75!black,colback=#1!5!white,title=JAVA application}
 {
  #2
  \tcblower
  #3
 }

% Command that should accept a JAVA code as a second mandatory. It leads to a compilation error when used.
\DeclareTotalTColorBox{\javaappwithminted}{ O{red}+m+v }{enhanced,colframe=#1!75!black,colback=#1!5!white,title=JAVA application}
 {
  #2
  \tcblower
  \scantokens{%
  \begin{tcblisting}{listing engine=minted,minted style=trac,minted language=java,
      colback=red!5!white,colframe=red!75!black,listing only,enhanced,nobeforeafter,skin=enhancedmiddle jigsaw,interior hidden,leftrule=0mm,rightrule=0mm,boxsep=0mm,top=0mm,bottom=0mm}
    #3
    \end{tcblisting}
  }
}





\begin{document}

\javaappwithminted{%
  Foo
}{%
  public class HelloWorld {

    // A ’Hello World’ in Java

    public static void main(String[] args) {
      System.out.println("Hello World!");
    }
  }
 }


\begin{tcolorbox}[enhanced,colframe=red!75!black,colback=red!5!white,title=JAVA application]
  Write a 'Hello Word' java class
  \tcblower
  \begin{tcblisting}{listing engine=minted,minted style=trac,minted language=java,
      colback=red!5!white,colframe=red!75!black,listing only,enhanced,nobeforeafter,skin=enhancedmiddle jigsaw,interior hidden,leftrule=0mm,rightrule=0mm,boxsep=0mm,top=0mm,bottom=0mm}
    public class HelloWorld {
      // A ’Hello World’ in Java
      public static void main(String[] args) {
        System.out.println("Hello World!");
      }
}
\end{tcblisting}
\end{tcolorbox}

\end{document}

在此处输入图片描述

相关内容