如何防止 lstlisting 重置 \everypar?

如何防止 lstlisting 重置 \everypar?

我注意到 lstlisting 通过 {}! 重置了 \everypar,因此之前的 \everypar 在 lstlisting 之后无效。如何防止 lstlisting 环境重置 \everypar?换句话说,我想设置一个在文本中完全有效的 \everypar。

\documentclass{minimal}

\usepackage{lipsum}
\usepackage{listings}

\begin{document}
\everypar{\lq\lq{}EVERYPAR\rq\rq}

\lipsum[1-2]


\hrule 
\begin{lstlisting}
Listing Environment
\end{lstlisting}
\hrule 

\lipsum[3]

\end{document}

在此处输入图片描述

答案1

这其实不太安全,也可能不会完全满足你的要求,但它确实满足了你的要求,

\documentclass{minimal}

\usepackage{lipsum}
\usepackage{listings}

\begin{document}
\let\oldeverypar\everypar
\newtoks\everypar
\oldeverypar{\the\everypar\lq\lq{}EVERYPAR\rq\rq}


\lipsum[1-2]


\hrule 
\begin{lstlisting}
Listing Environment
\end{lstlisting}
\hrule 

\lipsum[3]

\end{document}

在此处输入图片描述

答案2

我建议通过 定义你自己的 listings 环境\lstnewenvironment。这允许一些可能感兴趣的钩子:

在此处输入图片描述

\documentclass{article}

\usepackage{lipsum}
\usepackage{listings,everyhook}

\lstnewenvironment{mylstlisting}[1][]
  {\SavePreHook{par}{\prelistingpar}%
   \ClearPreHook{par}\endgraf
   \noindent\rule{\linewidth}{.4pt}%
   \lstset{#1}}
  {\endgraf\noindent\rule[.5\baselineskip]{\linewidth}{.4pt}%
   \RestorePreHook{par}{\prelistingpar}}

\begin{document}

\PushPreHook{par}{``EVERYPAR''}

\lipsum[1-2]

\begin{mylstlisting}
Listing Environment
\end{mylstlisting}

\lipsum[3]

\end{document}

答案3

\documentclass{article}

\usepackage{lipsum}
\usepackage{listings}

\begin{document}
\everypar{\lq\lq{}EVERYPAR\rq\rq}

\lipsum[1-2]


\hrule\begingroup
\begin{lstlisting}
Listing Environment
\end{lstlisting}
\endgroup\hrule 

\lipsum[3]

\end{document}

在此处输入图片描述


\documentclass{article}

\usepackage{lipsum}
\usepackage{listings}

\newtoks\myeverypar
\begin{document}
\everypar{\lq\lq{}EVERYPAR\rq\rq}

\lipsum[1-2]


\hrule\begingroup
\begin{lstlisting}
Listing Environment
\end{lstlisting}
\global\myeverypar\expandafter{\the\everypar}\endgroup
\everypar\expandafter\expandafter\expandafter{\expandafter\the
\expandafter\myeverypar\expandafter\everypar\expandafter{\the\everypar}}\hrule 

\lipsum[3-4]

\end{document}

在此处输入图片描述


\documentclass{article}

\usepackage{lipsum}
\usepackage{listings}

\newtoks\myeverypar
\begin{document}
\everypar{\lq\lq{}EVERYPAR\rq\rq}

\lipsum[1-2]

\makeatletter

\hrule\myeverypar\expandafter{\the\everypar}%
\begin{lstlisting}
Listing Environment
\end{lstlisting}
\def\par {\@restorepar \clubpenalty \@clubpenalty \everypar
\expandafter{\the\myeverypar}\par \@endpefalse}\hrule

\lipsum[3-4]

\end{document}

在此处输入图片描述

相关内容