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?

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

| improve this answer | |
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

| improve this answer | |

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.