如何防止LTXexample
忽略段落缩进?
\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}
笔记
当我写这个问题的时候,我认为它与我的问题相关此处(点击)。事实上并非如此。由于历史原因,我保留了补丁代码,并建立了相关链接。
答案1
以下序言补丁(使用etoolbox
)提供所需的输出:
\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
首先,它将常规\parindent
(20pt
对于标准文档类)存储在 中\parindent@save
。然后,补丁在设置实际输入之前插入保存的段落缩进。缩进丢失的原因是因为代码是在 中设置的minipage
,它固有地设置\parindent
为\z@
(零)。
一种“侵入性较小”的方法是使用preset
提供的钩子listings
:
\newlength{\parindentsave}
\AtBeginDocument{\setlength{\parindentsave}{\parindent}}
\lstset{
%...
preset=\setlength{\parindent}{\parindentsave}
}