2

This is a follow-up to What does the \the\everypar do? and TikZ Overlay: Inject content at the start of the next paragraph?

From a quick grep -R '\\everypar' texmf-dist/tex/latex I can see that \everypar is used extensively in different packages. Is there a safe, agreed-upon way how to use \everypar to prepend some content to some paragraphs without breaking other packages in the process? I read that ConTeXt has \EveryPar, but I assume that doesn't exist in LaTeX.

Essentially I'd like to create a macro which prepends something only to the next paragraph started after it is called, but without breaking other packages along the way.

For example, is it possible to insert a custom hook into the token list, and remove it again later--while hoping that other packages don't clear the token list when you're not looking? Or is there no such thing in TeX/LaTeX?

5
  • The problem is, that many LaTeX internals might set \everypar and later reset it to be empty, for example every heading uses \@afterheading, which sets \everypar to prevent a page break after a heading and remove the first indent, and this doesn't append or prepend anything but just sets the entire \everypar, so you can't be sure that anything remains in \everypar. What do you want to achieve anyway?
    – Skillmon
    Sep 23, 2019 at 8:21
  • @Skillmon: The details are in tex.stackexchange.com/questions/509059. But essentially I want to create a macro which just prepends something to the next paragraph started after it is called, and without breaking other packages along the way. It doesn't really matter if the content stays in there for a long time.
    – Fritz
    Sep 23, 2019 at 8:23
  • Wrapfigure saves the contents of \everypar as a token list. You might look at how ithat package does it. Sep 23, 2019 at 14:55
  • Why can't you just put your prepended stuff at the start of the paragraph? Sep 23, 2019 at 17:10
  • @PeterWilson Because sometimes the next paragraph starts in a different file. For example: \PrependToNextPar{something} \include{some-figure.tex}.
    – Fritz
    Dec 6, 2019 at 8:22

2 Answers 2

3

The safest way is probably to use an inline heading eg the following which puts Zzzzz into the start of the following paragraph and then cleans up \everypar it does this in a way that works even if the paragraph starts in a group, as here.

\documentclass{article}

\begin{document}

\paragraph{Zzzzz}

\showthe\everypar

{abc}


{abc}

\end{document}

enter image description here

Technically this is a making a local definition of \@svsechd with the code you want to insert and setting some booleans to control the behaviour.

There are other ways of setting and restoring \everypar but any package that is messing with start of paragraph should (hopefully) already be detecting this first paragraph of a section case and doing the right thing, so I would either directly use \paragraph or a similar command using the \@startsection mechanism, or at least to copy enough of the definition shown by the \show\everypar if you run the above that your code looks to other packages like a standard latex run-in heading.

1
  • Thanks for the pointer to \paragraph. I forgot that there is a command available which does something very similar to what I want to achieve.
    – Fritz
    Sep 24, 2019 at 7:28
0

For the benefit of future readers, I'd like to refer to this LaTeX3 solution proposed by cfr in a similar question:

\usepackage{l3galley,xparse}
\ExplSyntaxOn
\tl_gput_right:Nn \g_galley_par_begin_hook_tl
{
  Any stuff that you want to put at the beginning of a paragraph.
}
\ExplSyntaxOff

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.