4

(the commented lines will be used later but as we can see in MWE, they aren't related with the problem)

Why is there a par indent after the code box?

enter image description here

MWE

\documentclass{report}
\usepackage{lipsum}

\usepackage{tcolorbox}
\tcbuselibrary{minted,breakable,xparse,skins}

\DeclareTCBListing{myminted}{O{}m!O{}}{%
  listing only,
  minted language=#2,
  minted options={%
    #1},
  #3}

\begin{document}
\lipsum[2]
\begin{myminted}{R}
p1 = graph_plot(g)
p2 = f_omega_cycle(g)
p1 + p2
\end{myminted}
\lipsum*[2]
\end{document}
10
  • Manual says that autoparskip is the package default, in which case the after code is set to after={\par}, does either after={} or nobeforeafter help?
    – Scott H.
    Feb 10, 2020 at 18:59
  • @ScottH., no. Both pull back the text so it appears on the right of the box, out of margin.
    – Sigur
    Feb 10, 2020 at 19:02
  • Ah, that's too bad.
    – Scott H.
    Feb 10, 2020 at 19:04
  • I got these error Package minted Error: You must have "pygmentize" installed to use this package
    – AndréC
    Feb 10, 2020 at 19:06
  • @AndréC, yes. You need to install it: pip install Pygments It is a Python module.
    – Sigur
    Feb 10, 2020 at 19:08

2 Answers 2

6

By default, if you don't change /tcb/before nor /tcb/after, tcolorbox uses the /tcb/autoparskip style, which does the following:

  • If the length of \parskip is greater than 0pt at the beginning of the document, the /tcb/parskip style is executed.

  • Otherwise, /tcb/noparskip is executed.

(see Embedding into the Surroundings in the tcolorbox manual). In your case, we can check with \showthe\parskip after \begin{document}, which prints the following lines on the terminal:

> 0.0pt plus 1.0pt.
l.44 \begin{document}\showthe\parskip

When used in a comparison of 〈dimen〉s, this won't be greater than 0pt (the stretch component is thrown away). You can verify this with \ifdim\parskip>0pt Y\else N\fi, which will leave an N in the input stream. So, given your settings, since your box specifies neither /tcb/before nor /tcb/after, the /tcb/noparskip style will be executed. The tcolorbox manual tells us that this style is similar to:

\tcbset{
  noparskip/.style={
    before={\par\pagebreak[0]\smallskip\parindent=0pt},
    after={\par\smallskip}
  }
}

therefore tcolorbox automatically adds \par\smallskip after the box. As soon as the \lipsum* following your environment outputs a box, this starts a new paragraph, and since \noindent hasn't been used, you get normal paragraph indentation.

In case you don't want this indentation, you can use after={\par\smallskip\noindent} or after={\par\noindent} (depending on the desired vertical spacing) in your box options.

\documentclass{article}
\usepackage{lipsum}

\usepackage{tcolorbox}
\tcbuselibrary{minted,xparse}

\DeclareTCBListing{myminted}{O{}m!O{}}{%
  after={\par\smallskip\noindent},
  listing only,
  minted language=#2,
  minted options={#1},
  #3}

\begin{document}

\lipsum[2]
\begin{myminted}{R}
p1 = graph_plot(g)
p2 = f_omega_cycle(g)
p1 + p2
\end{myminted}
\lipsum*[2]

\end{document}

enter image description here

7
  • Thanks for your nice explanation. I'll read it carefully. Also, I edited the post to delete comments. My fault.
    – Sigur
    Feb 10, 2020 at 19:20
  • No problem, next time will be perfect. :-)
    – frougon
    Feb 10, 2020 at 19:21
  • How to make sure that the space between boxes and texts are well inserted? See the output here (with and without \smallskip)
    – Sigur
    Feb 10, 2020 at 19:28
  • \smallskip is... small. Did you try with \bigskip? Seems to work well here. (The skip can be discarded at page breaks, by design. You can use \nobreak before \smallskip if you really want white space after a box that is located at the end of a page, but this is most probably not what you want.)
    – frougon
    Feb 10, 2020 at 19:30
  • Well, it would be good to have the correct spacing, like in displayed blocks, stretchable when necessary. I'm not sure, but sounds that a new par there is not what LaTeX would do around an equation, for example. Maybe belowdisplayskip. Lets see.
    – Sigur
    Feb 10, 2020 at 19:33
4

The following uses the LaTeX2e internal \@endpetrue, that is used to let \end{<env>} use a \@doendpe if the environment specifies it (such as itemize for example), to suppress the paragraph indent if there is no empty line after your myminted, but there will be an indent if you leave a blank line.

\documentclass{report}
\usepackage{lipsum}

\usepackage{tcolorbox}
\tcbuselibrary{minted,breakable,xparse,skins}

\makeatletter
\newcommand*\mynoindent
  {%
    \par
    \@endpetrue
  }
\makeatother

%\definecolor{bg}{gray}{0.95}
\DeclareTCBListing{myminted}{O{}m!O{}}{%
%  breakable=true,
%  listing engine=minted,
  listing only,
  minted language=#2,
%  minted style=default,
  minted options={%
%    linenos,
%    gobble=0,
%    breaklines=true,
%    breakafter=,,
%    fontsize=\small,
%    numbersep=8pt,
    #1},
%  boxsep=0pt,
%  left skip=0pt,
%  right skip=0pt,
%  left=25pt,
%  right=0pt,
%  top=3pt,
%  bottom=3pt,
%  arc=5pt,
%  leftrule=0pt,
%  rightrule=0pt,
%  bottomrule=2pt,
%  toprule=2pt,
%  colback=bg,
%  colframe=orange!70,
%  enhanced,
%  overlay={%
%    \begin{tcbclipinterior}
%    \fill[orange!20!white] (frame.south west) rectangle ([xshift=20pt]frame.north west);
%    \end{tcbclipinterior}},
    after={\mynoindent},
  #3}

\begin{document}
\lipsum[2]
\begin{myminted}{R}
p1 = graph_plot(g)
p2 = f_omega_cycle(g)
p1 + p2
\end{myminted}
\lipsum*[2]
\begin{myminted}{R}
p1 = graph_plot(g)
p2 = f_omega_cycle(g)
p1 + p2
\end{myminted}

\lipsum*[2]
\end{document}

Example output:

enter image description here

3
  • Is there an analogous of \@doendpe for \begin{<env>}?
    – Sigur
    Feb 10, 2020 at 19:43
  • @Sigur you mean to suppress the indentation? I usually use \@afterindentfalse\@afterheading in that place. Technically, what you do when you're coding the code for an environment is not to call \@doendpe but instead \@endpetrue and the code in \end does invoke the \@doendpe.
    – Skillmon
    Feb 10, 2020 at 19:57
  • @Sigur I changed it to use \@endpetrue, I didn't test whether it'll work, as I didn't know where tcolorbox will use that code. Now I did actually test it, and it seems to work as well, so I opt for the "cleaner" approach of letting \end handle the rest.
    – Skillmon
    Feb 10, 2020 at 20:00

You must log in to answer this question.

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