this post was submitted on 23 Jun 2023
1 points (100.0% liked)

TeX typesetting

152 readers
1 users here now

A place to share ideas, resources, tips, and hacks for Donald Knuths typesetting software TeX. All variants and formats like OpTeX, LaTeX and ConTeXt are welcome.

founded 1 year ago
MODERATORS
 

Sometimes it's nice to have a macro that prevents indentation of the next paragraph. For example, if you're writing your own section header macros and it's hard to automatically insert a \noindent in the right place. There's another way but it's not very intuitive:

\def\firstnoindent{%
    \global\everypar={%
        \global\everypar={}%
        \setbox0=\lastbox
    }%
}

When I first came across this I had to pause and think about it a little.

What happens when you start a new paragraph? First you're in vertical mode, then TeX interprets some tokens as horizontal material and wants to switch over to horizontal mode. \noindent is a special horizontal material that starts an empty horizontal list, but all other material, like regular characters or horizontal glue, will first insert an \hbox of \parindent length. This is your normal paragraph indentation. Next, TeX will insert the content of \everypar, and then come your words or whatever made TeX start a paragraph. This order is important. \hbox - \everypar - .

When you place \firstnoindent somewhere, it sets \everypar to contain two things. 1) An instruction to set \everypar to be empty. This makes sure that only the next paragraph is affected. 2) It sets \box0 to contain the last box created, which makes the last box disappear.

So this is now in \everypar, but it won't be expanded until a paragraph is created.

When the next paragraph is created, TeX inserted the indentation \hbox, then the content of \everypar, which wipes \everypar and does the other thing: \setbox0=\lastbox.

\lastbox is a primitive that removes the last box, then immediately inserts it again. By itself it changes nothing. The idea is that you can make it disappear as the input to a \setbox, do things to that box, then manually put it back with \box. But here we don't put it back, it just disappears. And the last created box was the indentation \hbox.

So, after putting \firstnoindent somewhere, we'll make the next indentation disappear, and the next paragraph after that \everypar will be empty, and will indent as usual.

Caveat 1: This assumes that \everypar is normally empty.

Caveat 2: I'm on my phone and haven't tested the code, but it should work, in theory, maybe.

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here