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