I am trying to center a lstlisting
environment using tcolorbox
(as suggested here). The method does work normally but doesn't render as expected in enumerate
environment as shown below:
I have tried the following and tweaked it many times but nothing worked.
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{listings}
\usepackage{enumitem}
\usepackage[most]{tcolorbox}
\newenvironment{code}[1][]{%
\tcbwritetemp}%
{\endtcbwritetemp%
\tcbox[before=\begin{center},after=\end{center}]%
{\tcbusetemplisting}%
}%
\begin{document}
Start a list:
\begin{enumerate}[label = \textbf{--}]
\item Inside enumerate not working.
\begin{code}
if(x == 0){
return 0;
}else{
return 1;
}\end{code}
\end{enumerate}
This is what I want to achieve inside enumerate environment
\begin{code}
if(x == 0){
return 0;
}else{
return 1;
}
\end{code}
\end{document}
How can I achieve the desired output while roughly keeping the same outline of the logic used?