具有可选标题的 TColorBox 环境

具有可选标题的 TColorBox 环境

我正在尝试创建一个环境,允许显示带有标题作为可选参数的代码片段。如果创建了两个不同的环境,MWE 提供的修复了这个问题。我想将它们合并为一个。

这里使用的方法可能不那么简单,因为它也解决了遇到的问题这里。我还愿意接受任何其他可能更合适/更简单的方法。提前谢谢您!

输出

\documentclass{article}
\usepackage{enumitem}
\usepackage[most]{tcolorbox}

\makeatletter
\newenvironment{code}[2][]{%
    \@totalleftmargin=0pt%
    \newcommand{\foot}{#2} 
    \tcbwritetemp}%
    {\endtcbwritetemp%
    \tcbox[title = {\foot}, % The title option is present here
           before = \begin{center}, after = \end{center},
           fonttitle = \color{white}\itshape\ttfamily, colframe = blue, 
           listing only, listing options = {language = c++}
           ]%
    {\tcbusetemplisting}%
    }%
\makeatother

% This is included just to show how the final should look like
\makeatletter
\newenvironment{HowBoxSohuldLookLikeWithoutTitle}[2][]{%
    \@totalleftmargin=0pt%
    \newcommand{\foot}{#2}
    \tcbwritetemp}%
    {\endtcbwritetemp%
    \tcbox[ % The title option is removed and hence, no title bar is shown
           before = \begin{center}, after = \end{center},
           fonttitle = \color{white}\itshape\ttfamily, colframe = blue, 
           listing only, listing options = {language = c++}
           ]%
    {\tcbusetemplisting}%
    }%
\makeatother
\begin{document}    
    Start a list:
  \begin{enumerate}[label = \textbf{--}]
        \item Code with title:
        \begin{code}{The Title of Code}
#include <stdio.h>

int main()
{
    return 0;
}
        \end{code}      
        \item Code without title:
        \begin{code}
if(x == 0){
    return 0;
}else{
    return 1;
}
        \end{code}
        \item My goal is to have code without title to look like the following:
        \begin{HowBoxSohuldLookLikeWithoutTitle}
if(x == 0){
    return 0;
}else{
    return 1;
}
        \end{HowBoxSohuldLookLikeWithoutTitle}
    \end{enumerate}
\end{document}

答案1

您目前没有使用可选参数,而是必须只为您的环境使用 1 个参数,并将该参数默认为空。然后在环境的第二部分中测试是否\foot为空,如果是,则不要使用密钥title,否则使用它:

\documentclass{article}
\usepackage{enumitem}
\usepackage[most]{tcolorbox}

\makeatletter
\newenvironment{code}[1][]{%
    \@totalleftmargin=0pt%
    \def\foot{#1} 
    \tcbwritetemp}%
    {\endtcbwritetemp%
      \ifx\foot\@empty
        \tcbox[before = \begin{center}, after = \end{center},
               fonttitle = \color{white}\itshape\ttfamily, colframe = blue, 
               listing only, listing options = {language = c++}
               ]%
        {\tcbusetemplisting}%
      \else
        \tcbox[title = {\foot}, % The title option is present here
               before = \begin{center}, after = \end{center},
               fonttitle = \color{white}\itshape\ttfamily, colframe = blue, 
               listing only, listing options = {language = c++}
               ]%
        {\tcbusetemplisting}%
      \fi
    }%
\makeatother

\begin{document}    
    Start a list:
  \begin{enumerate}[label = \textbf{--}]
        \item Code with title:
        \begin{code}[The Title of Code]
#include <stdio.h>

int main()
{
    return 0;
}
        \end{code}      
        \item Code without title:
        \begin{code}
if(x == 0){
    return 0;
}else{
    return 1;
}
        \end{code}
    \end{enumerate}
\end{document}

在此处输入图片描述

相关内容