最小示例
\documentclass{report}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{caption}
\definecolor{captioncolor}{rgb}{0.7, 0.7, 0.7}
\definecolor{backgroundcolor}{rgb}{0.95, 0.95, 0.92}
\definecolor{commentcolor}{rgb}{0.55, 0.55, 0.55}
\definecolor{identifiercolor}{rgb}{0, 0, 0}
\definecolor{keywordcolor}{rgb}{0, 0, 0.5}
\definecolor{numbercolor}{rgb}{0, 0, 0}
\definecolor{stringcolor}{rgb}{0, 0.5, 0}
\newcounter{javacounter}
\stepcounter{javacounter}
% Customize captions of listings
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{captioncolor}{\parbox{\dimexpr\linewidth-2\fboxsep\relax}{#1#2#3}}}
\captionsetup[lstlisting]{
format = listing,
labelfont = white,
singlelinecheck = false, % Needed to not increase the counters by twice
textfont = white
}
% Styles
\lstdefinestyle{Java}{
basicstyle = \linespread{1.1}\mdseries,
backgroundcolor = \color{backgroundcolor},
breaklines = true,
commentstyle = \color{commentcolor},
identifierstyle = \color{identifiercolor},
keywordstyle = \color{keywordcolor},
language = Java,
literate =
{\_}{}{0\discretionary{\_}{}{\_}}
{Ä}{{\"A}}1
{Ö}{{\"O}}1
{Ü}{{\"U}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
{ü}{{\"u}}1
{ß}{{\ss}}1,
morekeywords = {assert},
numbers = left,
numbersep = 5pt,
numberstyle=\color{numbercolor},
showspaces = false,
showstringspaces = false,
showtabs = false,
stringstyle=\color{stringcolor},
tabsize = 2,
title = Java~(\thejavacounter):~\lst@@caption\stepcounter{javacounter},
xleftmargin = 15pt
}
\begin{document}
\begin{lstlisting}[style = Java]
public void main(String[] args) {
System.out.println("Hello World")
}
\end{lstlisting}
\begin{itemize}
\item Itemize test
\begin{lstlisting}[style = Java]
public void main(String[] args) {
System.out.println("Hello World")
}
\end{lstlisting}
\end{itemize}
\end{document}
我在这里遇到的问题是,应该在 itemize 中调整列表的位置。它应该缩进。有人知道如何实现吗?
提前致谢。
最小示例:
期望结果:
替代的首选结果:
答案1
问题不太清楚。您希望得到以下结果吗?
编辑:根据新请求,似乎感兴趣的问题是:
由以下 MWE (最小工作示例) 生成:
\documentclass{report}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{caption}
\usepackage{enumitem} % <--- added
\definecolor{captioncolor}{rgb}{0.7, 0.7, 0.7}
\definecolor{backgroundcolor}{rgb}{0.95, 0.95, 0.92}
\definecolor{commentcolor}{rgb}{0.55, 0.55, 0.55}
\definecolor{identifiercolor}{rgb}{0, 0, 0}
\definecolor{keywordcolor}{rgb}{0, 0, 0.5}
\definecolor{numbercolor}{rgb}{0, 0, 0}
\definecolor{stringcolor}{rgb}{0, 0.5, 0}
\newcounter{javacounter}
\stepcounter{javacounter}
% Customize captions of listings
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{captioncolor}{\parbox{\dimexpr\linewidth-2\fboxsep\relax}{#1#2#3}}}
\captionsetup[lstlisting]{
format = listing,
labelfont = white,
singlelinecheck = false, % Needed to not increase the counters by twice
textfont = white
}
% Styles
\lstdefinestyle{Java}{
basicstyle = \linespread{1.1}\mdseries,
backgroundcolor = \color{backgroundcolor},
breaklines = true,
commentstyle = \color{commentcolor},
identifierstyle = \color{identifiercolor},
keywordstyle = \color{keywordcolor},
language = Java,
literate =
{\_}{}{0\discretionary{\_}{}{\_}}
{Ä}{{\"A}}1
{Ö}{{\"O}}1
{Ü}{{\"U}}1
{ä}{{\"a}}1
{ö}{{\"o}}1
{ü}{{\"u}}1
{ß}{{\ss}}1,
morekeywords = {assert},
numbers = left,
numbersep = 5pt,
numberstyle=\color{numbercolor},
showspaces = false,
showstringspaces = false,
showtabs = false,
stringstyle=\color{stringcolor},
tabsize = 2,
title = Java~(\thejavacounter):~\lst@@caption\stepcounter{javacounter},
xleftmargin = 15pt
}
\begin{document}
\begin{lstlisting}[style = Java]
public void main(String[] args) {
System.out.println("Hello World")
}
\end{lstlisting}
\begin{itemize}[leftmargin=15pt] % <--- changed
\item Itemize test
\begin{minipage}{\linewidth}
\begin{lstlisting}[style = Java]
public void main(String[] args) {
System.out.println("Hello World")
}
\end{lstlisting}
\end{minipage}
\end{itemize}
\end{document}