3

I am writing a document containing lost of snippets of code, and I've defined an environment using tcblisting from the tcolorbox package to do this. The environments work fine.

What I want to do though is to suppress paragraph indentation afterwards so that

\begin{mycode}
   lots of stuff using my environment
\end{mycode}

which shows that Santa Claus \emph{does} exist,\ldots

has the effect of not indenting "which shows that..." I understand that the after parameter is what I need here; this is described in the tcolorbox manual (for v3.12) in section 3.12 (page 62). However, I can't get the result I want; for example after={\par\baselineskip\parindent=0pt} doesn't work.

I can go through and put a \noindent at all the places, but surely there's a way I can do it in the environment definition itself?

Is there a simple way to do this?

  • Could you provide some code we can compile, starting with \documentclass and ending with \end{document}? It'll get you answers much faster than providing code-snippets. – Werner Jan 20 '15 at 2:16
  • A blank line is a paragraph break. – cfr Jan 20 '15 at 2:38
  • With a blank line before the text you are signalling a new paragraph. If you do not put a blank line there then latex environments such as verbatim or enumerate would not indent the text. It appears tcblisting doesn't implement that which is a shame, but Harish's answer provides a workaround – David Carlisle Jan 20 '15 at 12:21
  • Yes, I think using a blank line and expecting no indentation is asking a bit much, so Harish's approach, with a % for readability, seems the way to go. Thanks very much! – Alasdair Jan 21 '15 at 0:48
  • Related tex.stackexchange.com/q/112404 – clemens May 12 '16 at 12:54
5

Use after={\par\vspace{\baselineskip}\noindent} and then

\end{code}
which shows that Santa Claus \emph{does} exist,\ldots

(no blank line after \end{code}) or

\end{code}
%
which shows that Santa Claus \emph{does} exist,\ldots

if you need that blank for readability.

\documentclass{article}
\usepackage{xcolor}
\usepackage[many]{tcolorbox}
\tcbuselibrary{listings}

\definecolor{light-gray}{gray}{0.95}

\newtcblisting{code}[1][]{
  width=\linewidth,
  enhanced,
  boxrule=0.4pt,
  colback=light-gray,
  listing only,
  top=0pt,
  bottom=0pt,
  listing options={
    basicstyle=\footnotesize\ttfamily,
    language=python,
    showstringspaces=false,
  },
  after={\par\vspace{\baselineskip}\noindent}  %% do you really need \vspace{\baselineskip}?
}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

\begin{code}
for i in range(n):
    print('i = ', i)
\end{code}
%
which shows that Santa Claus \emph{does} exist,\ldots    

\end{document}

enter image description here

  • Many thanks! I think I was in error expecting a black line with no indentation; I will take your advice and put a % sign there for readability, as well as adjusting my after= parameter. – Alasdair Jan 21 '15 at 0:47
2

A bit old, but as I just thought about this problem in another context. You can try this to suppress the parindent even if there is an empty line after the code:

\documentclass{article}

\usepackage[many]{tcolorbox}

\makeatletter
\let\@oridoenpe\@doendpe
\def\@newdoendpe{%
     \@endpetrue
     \def\par{\@restorepar\global\let\@doendpe\@oridoenpe
              \clubpenalty\@clubpenalty
              \everypar
               {{\setbox\z@\lastbox}%
                \everypar{}}\par\@endpefalse}%
     \everypar{{\setbox\z@\lastbox}%
                \everypar{}\@restorepar\@endpefalse
               }}

\newcommand\usenewdoendpe{\global\let\@doendpe\@newdoendpe}
\newcommand\useendparenv{\par\@endparenv}
\makeatother

\tcbuselibrary{listings}

\definecolor{light-gray}{gray}{0.95}

\newtcblisting{code}[1][]{
  after={\usenewdoendpe\smallskip\useendparenv}  %% 
}

\begin{document}
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

\begin{code}
for i in range(n):
    print('i = ', i)
\end{code}

which shows that Santa Claus \emph{does} exist,\ldots

\begin{code}
for i in range(n):
    print('i = ', i)
\end{code}
which shows that Santa Claus \emph{does} exist,\ldots


\end{document}

enter image description here

Edit

It won't work for breakable boxes (even if they are not broken).

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.