在列表内部时,我的 lstlistings 环境代码的标题对齐不正确。
工作示例中给出了生成图像的代码...
\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}
我怎样才能解决这个问题?
更新
我发现我可以将它们包装在tcolorbox
环境中,例如......
\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}
有没有办法将这两者结合到单一code
环境中(不使用 tcblistings)?
我试过...
\usepackage{etoolbox}
\BeforeBeginEnvironment{lstcode}{\begin{tcolorbox}[size=fbox,breakable, standard jigsaw, opacityback=0, frame hidden, sharp corners, enhanced, boxrule=0pt]}
\AfterEndEnvironment{lstcode}{\end{tcolorbox}}
并最终得到一个可行示例...
\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}
所以现在我只需要修复列表下的缩进和多余的代码行......
更新
当我将上述修复程序放入实际文档中时,对齐问题就消失了。最后一个问题是,当有标题时,tcolorbox 的顶部太大(如下所示,带框架)。
我怎样才能解决这个问题?