8

How to prevent LTXexample from ignoring the paragraph indentation?

\documentclass[preview,border=3pt]{standalone}
\usepackage{etoolbox}
\edef\keptparindent{\the\parindent}
\patchcmd{\preview}
  {\ignorespaces} %%% \preview ends with \ignorespaces (egreg's patch)
  {\parindent\keptparindent\ignorespaces}
  {}{}

\usepackage{accsupp}
\newcommand*{\noaccsupp}[1]{\BeginAccSupp{ActualText={}}#1\EndAccSupp{}}

\usepackage{xcolor}
\usepackage{showexpl}
\lstset
{
    numbers=left,
    numbersep=1em,
    numberstyle=\tiny\color{white}\noaccsupp,% to hide number lines
    frame=single,
    framesep=\fboxsep,% expands outward, cannot affect if frame=none
    framerule=\fboxrule,% expands outward, cannot affect if frame=none
    rulecolor=\color{red},% cannot affect if frame=none
    xleftmargin=\dimexpr\fboxsep+\fboxrule\relax,
    xrightmargin=\dimexpr\fboxsep+\fboxrule\relax,
    breaklines=true,
    breakindent=0pt,
    tabsize=2,
    columns=flexible,
    language={[LaTeX]TeX},
    basicstyle=\small\ttfamily\hbox{},
    keywordstyle=\color{blue},
    backgroundcolor=\color{cyan!10},
    pos=b,
    explpreset={},
}

\begin{document}
\begin{LTXexample}
The position of a particle moving along the $x$-axis is given as
\[
s_t=s_0+v_0t+\frac 1 2 at^2
\]
where $s_0$, $v_0$, $a$, and $t$ represent the initial position, initial speed, acceleration, and the time, respectively.
Even though you are not interested in physics, please be quiet.

And now \ldots
\end{LTXexample}

Indented line \ldots (egreg's patch works!)

\noindent Noindented line \ldots
\end{document}

enter image description here

Note

At the time I wrote this question I thought it was relevant to my problem here (click). In fact it was not. I leave the patch code intact for a historical reason and make a related link work.

1 Answer 1

5

The following preamble patch (using etoolbox) provides the desired output:

\makeatletter
\newlength{\parindent@save}
\AtBeginDocument{\setlength{\parindent@save}{\parindent}}
\patchcmd{\SX@put@code@result}% <cmd>
  {\SX@resultInput}% <search>
  {\setlength{\parindent}{\parindent@save}\SX@resultInput}% <replace>
  {}{}% <success><failure>
\makeatother

First off, it stores the regular \parindent (20pt for the standard document classes) in \parindent@save. Then, the patch inserts the saved paragraph indent before the actual input is set. The reason for the loss of indent is because the code is set in a minipage, which inherently sets \parindent to \z@ (zero).

enter image description here

A "less invasive" method would be to use the preset hook provided by listings:

\newlength{\parindentsave}
\AtBeginDocument{\setlength{\parindentsave}{\parindent}}
\lstset{
  %...
  preset=\setlength{\parindent}{\parindentsave}
}
2
  • 1
    Instead of patching \SX@put@code@result setting \lstset{preset=\setlength{\parindent}{\parindent@save}} should suffice. The preset key description says “Any TeX code executed before the sample code but not visible in the listings area.”
    – cgnieder
    Commented Jun 8, 2013 at 17:09
  • @cgnieder: I use your code. Danke! Commented Jun 8, 2013 at 23:53

You must log in to answer this question.

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