0

With the following code I get some glitches in the background color of my listing environment:

enter image description here

How can I avoid these glitches? Aside from this issue, the margin top and bottom is different. The code box should be centered between the two paragraphs.

\documentclass{article}
\usepackage{fontspec}
\usepackage{listings}
\usepackage{lipsum}
\usepackage{xcolor}
\usepackage{xeCJK}
\usepackage{parskip}
\usepackage{lstfiracode}

\setCJKmainfont{ipaexm.ttf}
\setmonofont{Fira Code}

\lstset{
  language=C,
  style=FiraCodeStyle,
  basicstyle=\setlength{\lineskip}{0pt}\ttfamily\footnotesize,
  commentstyle=\rmfamily\itshape\color{gray},
  backgroundcolor=\color{lightgray},
  frame=single,
  framerule=0pt,
  framextopmargin=3ex,
  framexbottommargin=3ex,
  framexleftmargin=1em,
  xleftmargin={\dimexpr 1em+3pt},
  linewidth={\dimexpr \linewidth-3pt}
}

\begin{document}
\lipsum[1][1-3]
\begin{lstlisting}
int main(int argc, char * argv[]) {
    printf("hello, world!\n");
}
\end{lstlisting}
\lipsum[2][1-3]
\end{document}
1
  • use tcolorbox to create listings with colored backgrounds. It works much better. Commented Aug 6, 2020 at 15:55

1 Answer 1

1

Major Edit

I have rewritten the entire example using tcolorbox and minted. I'll leave the old solution here, since it works ok and comes closer to the original requirements.

If pygments is neither installed nor in the path

Update your TeX-Distribution. Until the installation and/or path issues aren't resolved, this can be used:

latexmk -lualatex -shell-escape foo.tex
\documentclass{article}
\usepackage{fontspec}
\usepackage{blindtext}
% \usepackage{xeCJK}
\usepackage{parskip}
\usepackage{lstfiracode}
\usepackage[
    listings
    % minted
]{tcolorbox}
% \usepackage{minted}

% \setCJKmainfont{ipaexm.ttf}
% \setmonofont{Fira Code}

\begin{document}
\blindtext[1]

\begin{tcblisting}{
    % listing engine=minted,
    % minted style=trac,
    % minted language=c,
    listing only,
    boxrule=1pt,
}
int main(int argc, char * argv[]) {
    printf("hello, world!\n");
}
\end{tcblisting}

\blindtext[2]
\end{document}

The new version

This offers beautiful syntax highlighting and leaves almost nothing to the imagination:

\documentclass{article}
\usepackage{fontspec}
\usepackage{lipsum}
% \usepackage{xeCJK}
\usepackage{parskip}
\usepackage{lstfiracode}
\usepackage[most,minted]{tcolorbox}
\usepackage{minted}

% \setCJKmainfont{ipaexm.ttf}
% \setmonofont{Fira Code}

\begin{document}
\lipsum[1][1-3]

\begin{tcblisting}{
    listing engine=minted,
    minted style=trac,
    minted language=c,
    listing only,
    boxrule=1pt,
}
int main(int argc, char * argv[]) {
    printf("hello, world!\n");
}
\end{tcblisting}

\lipsum[2][1-3]
\end{document}

The output is really nice and highly customizable:

New Listing with tcolorbox and minted

The old version

Well, there have been many solutions for your problem:

and even here:

Yet, there always seemed to be a catch. This here seems to work quite reliably:

\documentclass{article}
\usepackage{fontspec}
\usepackage{listings}
\usepackage{adjustbox}
\usepackage{lipsum}
\usepackage{xcolor}
% \usepackage{xeCJK}
\usepackage{parskip}
\usepackage{lstfiracode}
\usepackage[framemethod=tikz]{mdframed}

% \setCJKmainfont{ipaexm.ttf}
% \setmonofont{Fira Code}

\definecolor{light-gray}{gray}{0.75}

\lstset{
  language=C,
  style=FiraCodeStyle,
  basicstyle=\setlength{\lineskip}{0pt}\ttfamily\footnotesize,
  commentstyle=\rmfamily\itshape\color{gray},
%   frame=single,
%   framerule=0pt,
%   framextopmargin=3ex,
%   framexbottommargin=3ex,
%   framexleftmargin=1em,
%   xleftmargin={\dimexpr 1em+3pt},
%   linewidth={\dimexpr \linewidth-3pt},
  aboveskip=1.5em,
  belowskip=1em,
}

\surroundwithmdframed[
  hidealllines=true,
  backgroundcolor=light-gray,
  innerleftmargin=15pt,
  innertopmargin=0pt,
  innerbottommargin=0pt]{lstlisting}

\mdfsetup{skipabove=1.5em,skipbelow=1em}

\begin{document}
\lipsum[1][1-3]

\begin{lstlisting}
int main(int argc, char * argv[]) {
    printf("hello, world!\n");
}
\end{lstlisting}

\lipsum[2][1-3]
\end{document}

And the output:

enter image description here

9
  • 1
    I wouldn't use mdframed, it is old, in part buggy and unmaintained for quite some time now. tcolorbox is much better. Commented Aug 6, 2020 at 17:31
  • @UlrikeFischer, thank you for being so helpful. In the last couple of days I've learned more than in the last two years! I have rewritten the entire mwe and will edit right away. Commented Aug 6, 2020 at 17:57
  • With your first solution I have an issue when using latexmk -xelatex foo.tex. Package minted Error: You must invoke LaTeX with the -shell-escape flag.. Why should I use the shell-escape and how should I use it?
    – nowox
    Commented Aug 6, 2020 at 20:29
  • @phil-elkabat with -shell-escape I now have this error Package minted Error: Missing Pygments output;
    – nowox
    Commented Aug 6, 2020 at 20:33
  • Is your TeX-Distribution up to date? What OS are you on? Commented Aug 6, 2020 at 20:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .