0

I am trying to make an environment that allows the display of code snippet with title as optional parameter. The MWE provided fixes the issue if two different environments were created. I would like to combine them into one.

The approach used here might not be as straightforward because it also fixes the issue encountered here. I am also open to any other approaches that might be more suitable/less complicated. Thank you in advance!

Output

\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}
2

You're currently not using the optional argument as such, instead you'd have to only use 1 argument for your environment and have that one default to being empty. Then in the second part of your environment you test whether \foot is empty, if so don't use the title key, else use it:

\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}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.