2

As I noticed lstlisting reset \everypar by {}! so previous \everypar has no effect after lstlisting. How to prevent lstlisting environment from reseting \everypar? in other words, I want to set an \everypar that works throughly in the text.

\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}

enter image description here

1
  • lstlisting like almost all latex displays is a list and lists and section headings reset everypar to control indentation Nov 7, 2016 at 21:07

3 Answers 3

5

this isn't really safe and may not do quite what you want, but it does do what you asked for,

\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}

enter image description here

5
  • +1 for this answer... and particularly for the first sentence! ;-) Nov 7, 2016 at 21:51
  • This doesn't answer the question in my opinion. The OP is interested in the output as-is from the lstlisting environment, but would like \everypar to continue as before lstlisting.
    – Werner
    Nov 7, 2016 at 21:59
  • @Werner sure but the question isn't really clear about exactly when the modified everypar is supposed to act (eg the example provided doesn't have section headings or a center environment etc) I could make listings "safe" but I doubt that would really be any better. If the question is clarified for real intent the real answer may not involve \everypar at all (despite the question title) Nov 7, 2016 at 22:02
  • @DavidCarlisle this solution affects all latex display even section! I wanted to set a special charachter at the begining of each paragraph of the text by \everypar but lstlisting and even section or something like that will reset it. Suppose I want to count all paragraphs in a text with the aid of \everypar. I want to know is it possible or not? something like a linenumber that listings uses for counting its lines but in my case for counting all paragraphs of a text.
    – javadr
    Nov 8, 2016 at 5:47
  • @javadr that was the point of my answer \everypar is a low level command that affects every low level paragraph that includes section headings, individual lines forced by \\ in center and similar environments and lots of other places. It is probably not the tool you need, but really you only asked about \everypar you haven#t given a clear indication intention of the final formatting (which may for example be best done as a list with your text added by \item) Nov 8, 2016 at 7:52
2

I would suggest defining your own listings environment via \lstnewenvironment. This allows some hooks that might be of interest:

enter image description here

\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}
1
  • this makes listings sort of safe but add \section{kkkk}kkk and EVERYPAR shows up (presumably unwanted) in the section heading, so unless you redefine everything it doesn't really help. Nov 7, 2016 at 22:42
2
\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}

enter image description here


\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}

enter image description here


\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}

enter image description here

3
  • yes but that also stops the the \everpar setting that listings set to work, so you lose control over indentation (not obvious in this example) add \section{kkkk} kkkkk for example. Nov 7, 2016 at 22:06
  • @DavidCarlisle see update. Actually I don't exactly know what the OP is after.
    – user4686
    Nov 7, 2016 at 22:17
  • probably not \everypar :-) Nov 7, 2016 at 22:43

You must log in to answer this question.

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