1

Minimal Example

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

The problem I have here is that the position of the listings should be adjusted in itemize. It should be indented. Anyone know how to achieve this?

Thanks in advance.

Minimal Example:

Example output

Preferred result:

Preferred result

Alternative preferred result:

Alternative preferred result

1

Question is not very clear. Do you like to have the following result?

Edit: According to new request, it seems that the matter of interest is:

enter image description here

which is produced by the following MWE (Minimal Working Example):

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

| improve this answer | |
  • The second one. The listing position should start where the itemize dot starts. – Nyanyan Oct 21 at 17:30
  • 1
    @Nyanyan, MWE (Minimal Working Example) for the selected image is added now. – Zarko Oct 21 at 17:36
  • Thanks for the solution. Is there a way to keep the position of the itemize but to adjust the position of the listing? I've added an example. – Nyanyan Oct 21 at 18:57
  • 1
    @Nyanyan, well, this now different story. See edited answer. – Zarko Oct 21 at 19:36
  • Thanks alot, can I ask u what your first example was again? – Nyanyan Oct 21 at 21:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.