1

The alignment of the caption of the my lstlistings environment for code is not correct when inside lists.

incorrect alignment of lstlistings environment inside items

The code to generate the image is given in the working example...

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{enumitem}

\usepackage{subcaption} \usepackage[hypcap=true]{caption} \captionsetup{justification=raggedright,singlelinecheck=true} \usepackage{capt-of}

% create non-copy numbers (source - https://tex.stackexchange.com/a/57160) \usepackage{accsupp} \newcommand\donotcopy[1]{% line number printing mechanism \BeginAccSupp{ActualText={}}#1\EndAccSupp{}% }

% define colors for code blocks \definecolor{codegreen}{rgb}{0,0.6,0} \definecolor{codegray}{rgb}{0.5,0.5,0.5} \definecolor{codepurple}{rgb}{0.58,0,0.82} \definecolor{codebackcolour}{rgb}{0.95,0.95,0.92}

% use listings for code block styling \usepackage{listings}

% define style for code snippets \lstdefinestyle{code}{ backgroundcolor=\color{gray!05},
commentstyle=\color{codegray}, keywordstyle=\color{blue}, numberstyle=\ttfamily\color{codegray}\tiny\donotcopy, stringstyle=\color{codegreen}, basicstyle=\ttfamily\footnotesize, breakatwhitespace=false, breaklines=true,
captionpos=t,
keepspaces=true, numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false, showtabs=false,
tabsize=2, frame=tb, escapeinside={!>}{<!}, extendedchars=true, % Literate one-to-one mapping of numerals (source - https://tex.stackexchange.com/a/153115) literate={0}{0}{1}% {1}{1}{1}% {2}{2}{1}% {3}{3}{1}% {4}{4}{1}% {5}{5}{1}% {6}{6}{1}% {7}{7}{1}% {8}{8}{1}% {9}{9}{1}% }

% caption setup for code % (source - https://tex.stackexchange.com/a/117538) \DeclareCaptionFormat{codecaptionformat}{ \rule{\dimexpr\textwidth\relax}{0.4pt} \par\vskip1pt#1#2#3} \captionsetup[code]{ format=codecaptionformat, singlelinecheck=false, margin=0pt, font={sf}, labelsep=period, % (Source - https://tex.stackexchange.com/a/215884) labelfont=bf }

% Creating the lstcode environment (Source - https://tex.stackexchange.com/a/483721) \lstnewenvironment{lstcode}[1][] {% \def\lstlistingname{Code Snippet}% \lstset{#1, style=code}% % \clearcaptionsetup{lstlisting}% % This was in the example to prevent format mixing \captionsetup{options=code} % }% {}

\begin{document}

Below is how this should look... \begin{lstcode}[language=Python,caption={Listings Example}] print("Hello") # comment \end{lstcode}

\begin{itemize} \item Item text above the code \begin{lstcode}[language=Python,caption={Listings Example}] print("Hello") # comment \end{lstcode} \end{itemize}

\end{document}

How can I fix this?

UPDATE I found out I can wrap these in a tcolorbox environment,eg...

\begin{tcolorbox}[width=\textwidth, breakable, standard jigsaw, opacityback=0, frame hidden, sharp corners, enhanced, boxrule=0pt]
\begin{lstcode}[language=Python,caption={Listings Example}]
print("Hello World")
\end{lstcode}
\end{tcolorbox}

Is there a way to combine both of these into a singular code environment (w/out using tcblistings)?

I tried...

\usepackage{etoolbox}

\BeforeBeginEnvironment{lstcode}{\begin{tcolorbox}[size=fbox,breakable, standard jigsaw, opacityback=0, frame hidden, sharp corners, enhanced, boxrule=0pt]} \AfterEndEnvironment{lstcode}{\end{tcolorbox}}

and ended up w/ a working example...

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{enumitem}
\usepackage[most]{tcolorbox}

\usepackage{subcaption} \usepackage[hypcap=true]{caption} \captionsetup{justification=raggedright,singlelinecheck=true} \usepackage{capt-of}

% create non-copy numbers (source - https://tex.stackexchange.com/a/57160) \usepackage{accsupp} \newcommand\donotcopy[1]{% line number printing mechanism \BeginAccSupp{ActualText={}}#1\EndAccSupp{}% }

% define colors for code blocks \definecolor{codegreen}{rgb}{0,0.6,0} \definecolor{codegray}{rgb}{0.5,0.5,0.5} \definecolor{codepurple}{rgb}{0.58,0,0.82} \definecolor{codebackcolour}{rgb}{0.95,0.95,0.92}

% use listings for code block styling \usepackage{listings}

% define style for code snippets \lstdefinestyle{code}{ backgroundcolor=\color{gray!05},
commentstyle=\color{codegray}, keywordstyle=\color{blue}, numberstyle=\ttfamily\color{codegray}\tiny\donotcopy, stringstyle=\color{orange}, basicstyle=\ttfamily\footnotesize, breakatwhitespace=false, breaklines=true,
captionpos=t,
keepspaces=true, numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false, showtabs=false,
tabsize=2, frame=tb, escapeinside={!>}{<!}, extendedchars=true, % Literate one-to-one mapping of numerals (source - https://tex.stackexchange.com/a/153115) literate={0}{0}{1}% {1}{1}{1}% {2}{2}{1}% {3}{3}{1}% {4}{4}{1}% {5}{5}{1}% {6}{6}{1}% {7}{7}{1}% {8}{8}{1}% {9}{9}{1}% }

% caption setup for code % (source - https://tex.stackexchange.com/a/117538) \DeclareCaptionFormat{codecaptionformat}{ \rule{\dimexpr\textwidth\relax}{0.4pt} \par\vskip1pt#1#2#3} \captionsetup[code]{ format=codecaptionformat, singlelinecheck=false, margin=0pt, font={sf}, labelsep=period, % (Source - https://tex.stackexchange.com/a/215884) labelfont=bf }

\usepackage{fancyvrb}

% % Creating the lstcode environment (Source - https://tex.stackexchange.com/a/483721) \lstnewenvironment{lstcode}[1][]% { \def\lstlistingname{Code Snippet}% \lstset{#1, style=code}% % \clearcaptionsetup{lstlisting}% % This was in the example to prevent format mixing \captionsetup{options=code} % }% {}

\usepackage{etoolbox}

\BeforeBeginEnvironment{lstcode}{\begin{tcolorbox}[size=fbox,breakable, standard jigsaw, opacityback=0, frame hidden, sharp corners, enhanced, boxrule=0pt]} \AfterEndEnvironment{lstcode}{\end{tcolorbox}}

\begin{document}

\begin{itemize} \item Item above some coe

\begin{lstcode}[language=Python, caption={Hello World Example}] print("Hello World")\end{lstcode} \begin{itemize} \item More code \begin{lstcode}[language=Python, caption={Hello World Example}] print("Hello World")\end{lstcode} \begin{itemize} \item More code \begin{lstcode}[language=Python, caption={Hello World Example}] print("Hello World") \end{lstcode} \begin{lstcode}[language=Python, caption={Hello World Example}] print("Hello World") \end{lstcode} \end{itemize} \end{itemize} \end{itemize}

\end{document}

and the output... etoolbox fix

So now I just need to fix the indentation and extra lines of code under lists...

UPDATE

The alignment issue goes away when I put the above fix in my actual document. The last problem problem is that the top of the tcolorbox is too big when there is a caption (shown below w/ frame).

frame issues

How can I fix this?

user41177
  • 375

0 Answers0