4

I tried to indent first paragraph after a customized lstlisting environment

\documentclass{article}
\usepackage{listings,lipsum}
\lstnewenvironment{mycode}{}{\par}
\begin{document}
\lipsum[1]
\begin{mycode}
\def\cmd#1{\cs{\expandafter\cmd@to@cs\string#1}}
\def\cmd@to@cs#1#2{\char\number`#2\relax}
\DeclareRobustCommand\cs[1]{\texttt{\char`\\#1}}
\end{mycode}
\lipsum[1]
\end{document}

But the above code fails. How could I fix the above code?

2 Answers 2

0

Use etoolbox and its \AfterEndEnvironment hook to insert \par

\documentclass{article}
\usepackage{listings,lipsum}
\lstnewenvironment{mycode}{}{}
\usepackage{etoolbox}
\AfterEndEnvironment{mycode}{\par}
\begin{document}
\lipsum[1]
\begin{mycode}
\def\cmd#1{\cs{\expandafter\cmd@to@cs\string#1}}
\def\cmd@to@cs#1#2{\char\number`#2\relax}
\DeclareRobustCommand\cs[1]{\texttt{\char`\\#1}}
\end{mycode}
\lipsum[1]
\end{document}

enter image description here

1

Just leave a blank line after the environment, if you want indentation:

\documentclass{article}
\usepackage{listings,lipsum}
\lstnewenvironment{mycode}{}{}

\begin{document}

\lipsum[1]
\begin{mycode}
\def\cmd#1{\cs{\expandafter\cmd@to@cs\string#1}}
\def\cmd@to@cs#1#2{\char\number`#2\relax}
\DeclareRobustCommand\cs[1]{\texttt{\char`\\#1}}
\end{mycode}

\lipsum[1]
\end{document}

enter image description here

You must log in to answer this question.

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