20

For debugging purposes, how can I have ¶ at the start of each paragraph automatically? (By paragraph I don't mean the \paragraph command, but simply two blocks of text separated by a blank line, as in the MWE below.)

While \everypar did not work for me (I am on TeX Live 2022), \AddToHook{para/begin}{\P~} did, in a way. It adds ¶ before every paragraph, including titles of section etc. where it is unwanted.

\documentclass{article}
\setlength{\parindent}{0em}

\begin{document}
\AddToHook{para/begin}{\P~}

\section*{This Sentence No Verb}

Far far away, behind the word mountains, far from the countries 
\emph{Vokalia} and \emph{Consonantia}, there live the blind texts.
Separated they live in Bookmarksgrove right at the coast of the Semantics, 
a large language ocean. The river Duden flows by their place and supplies 
it with the necessary regelialia. It is a paradisemantic country, in which 
roasted parts of sentences fly into your mouth.

Even the all-powerful Pointing has no control about the blind texts;
it is an almost unorthographic life. One day, however, a small line of 
blind text by the name of \textsc{Lorem Ipsum} decided to leave for the far 
World of Grammar. The Big Oxmox advised her not to do so, because there 
were thousands of bad Commas, wild Question Marks and devious Semikoli, but 
the Little Blind Text didn’t listen. She packed her seven versalia, put her 
initial into the belt and made herself on the way. 

When she reached the first hills of the Italic Mountains, she had a last 
view back on the skyline of her hometown Bookmarksgrove, the headline of 
Alphabet Village and the subline of her own road, the Line Lane.
\end{document} 

enter image description here

How can I restrict \AddToHook to plain-text paragraphs only?

2

3 Answers 3

18

There's no surefire way to determine if a "TeX paragraph" is a "real paragraph", but we can get pretty close by assuming that any indented paragraphs are "real":

\documentclass{article}

\setlength{\parindent}{1sp} % Tiny sentinel value

% \usepackage{indentfirst} % Optional, to mark the first paragraph of a section

\AddToHook{para/begin}{%
    \ifdim\wd\IndentBox=\parindent% Replace any indents with pilcrows
        \setbox\IndentBox=\hbox{\P~}%
    \fi%
}

\begin{document}

\section*{This Sentence No Verb}

Far far away, behind the word mountains, far from the countries
\emph{Vokalia} and \emph{Consonantia}, there live the blind texts.
Separated they live in Bookmarksgrove right at the coast of the Semantics,
a large language ocean. The river Duden flows by their place and supplies
it with the necessary regelialia. It is a paradisemantic country, in which
roasted parts of sentences fly into your mouth.

Even the all-powerful Pointing has no control about the blind texts;
it is an almost unorthographic life. One day, however, a small line of
blind text by the name of \textsc{Lorem Ipsum} decided to leave for the far
World of Grammar. The Big Oxmox advised her not to do so, because there
were thousands of bad Commas, wild Question Marks and devious Semikoli, but
the Little Blind Text didn’t listen. She packed her seven versalia, put her
initial into the belt and made herself on the way.

When she reached the first hills of the Italic Mountains, she had a last
view back on the skyline of her hometown Bookmarksgrove, the headline of
Alphabet Village and the subline of her own road, the Line Lane.

\begin{itemize}
    \item This is the first paragraph of an item.

    This is the second paragraph.

    \item This is the second item.
\end{itemize}

And this is a paragraph after an itemize.

\end{document}

output

Of course, even this isn't foolproof — for example, I'm not sure what the expected behaviour is in an itemize environment.

1
  • 3
    That's a nifty approach … I am not too worried about environments (itemize or others), as I can always make an exception for them with something like \AddToHook{env/enumerate/before}{\RemoveFromHook{para/begin}}.
    – Ingmar
    Jul 4, 2022 at 6:36
10

Building on Max' answer (which I have accepted), I would like to post a more fleshed-out solution.

\documentclass[draft]{article}
%\documentclass[final]{article}

\usepackage{indentfirst}
    \setlength{\parindent}{1em}

\usepackage{xcolor}
    \definecolor{Crimson}{HTML}{C00000}

% create \ifdraft{DRAFT}{FINAL} command:
\makeatletter\def\ifdraft{%
\ifdim\overfullrule>\z@\expandafter\@firstoftwo%
\else\expandafter\@secondoftwo\fi}%
\makeatother

% replace parindent with ¶ sign:
\newcommand\mypilcrow{%
\ifdim\wd\IndentBox=\parindent\sbox{\IndentBox}{\color{Crimson}\P~}\fi%
}

\begin{document}

% in DRAFT mode, execute \mypilcrow at beginn of paragraph,
% in FINAL mode, do nothing:
\ifdraft{\AddToHook{para/begin}{\mypilcrow}}{}

% Temporarily disable in \itemize environment if so desired:
%\AddToHook{env/itemize/before}{\RemoveFromHook{para/begin}}
%\AddToHook{env/itemize/after}{\AddToHook{para/begin}{\mypilcrow}}

\section*{This Sentence No Verb}

Far far away, behind the word mountains, far from the countries \emph
{Vokalia} and \emph{Consonantia}, there live the blind texts. Separated they
live in Bookmarksgrove right at the coast of the Semantics, a large language
ocean. The river Duden flows by their place and supplies it with the necessary
regelialia. It is a paradisemantic country, in which roasted parts of
sentences fly into your mouth.

Even the all-powerful Pointing has no control about the blind texts;
it is an almost unorthographic life. One day, however, a small line of blind
text by the name of \textsc{Lorem Ipsum} decided to leave for the far World of
Grammar. The Big Oxmox advised her not to do so, because there were thousands
of bad Commas, wild Question Marks and devious Semikoli, but the Little Blind
Text didn’t listen. She … 

\begin{itemize}
    \item packed her seven versalia, 
    \item put her initial into the belt and 
    \item made herself on the way. 

    She also added a new paragraph to the end of the last item.

\end{itemize}

When she reached the first hills of the Italic Mountains, she had a last view
back on the skyline of her hometown Bookmarksgrove, the headline of Alphabet
Village and the subline of her own road, the Line Lane.

\end{document}

enter image description here

7
  • 1
    I suggest you use \mbox rather than \hbox (the latter could cause trouble if color commands are used inside).
    – frougon
    Jul 4, 2022 at 7:58
  • Indeed, sorry. I propose to replace \setbox\IndentBox=\hbox{\P~} with \sbox{\IndentBox}{\P~}. \sbox is from the LaTeX kernel and color-safe, contrary to \hbox (TeX primitive). You can test with \usepackage{xcolor} and \sbox{\IndentBox}{\color{red}\P~} for instance.
    – frougon
    Jul 4, 2022 at 9:14
  • Thank you. I have modified my answer above.
    – Ingmar
    Jul 4, 2022 at 9:30
  • Quick note to others: This solution does not work with the KOMA-Script classes … But that's for another thread.
    – Ingmar
    Jul 4, 2022 at 9:58
  • 1
    Rather than removing and re-adding the hook, I would probably put some logic into it so that you can control it from the outside (eg setting a boolean switch) and put that into the list before/after hooks. Adding and removing hooks form the machinery is more expensive in execution time. Jul 5, 2022 at 7:23
6

Just a fragile and far-from-complete attempt.

\@startsection, the common starting point of sectioning title commands with levels not higher than \section (aka, not including \chapter and \part) is prepended a boolean assignment to signal "this paragraph is non-plain".

Thus this attempt may only work for vanilla standard classes without any sectioning title styling packages.

It's incomplete because there're all kinds of cases apart from sectioning titles, for example list items. Depending on your real use case, is it worth accomplishing a complete set of patches?

\documentclass{article}
\setlength{\parindent}{0em}

\usepackage{xpatch}

\makeatletter
\newif\if@plaintextpara
\global\@plaintextparatrue

\xpretocmd\@startsection
  {\@plaintextparafalse}
  {}{\PatchFailed}

\AddToHook{para/begin}{%
  \if@plaintextpara\P~\else\global\@plaintextparatrue\fi
}
\makeatother

\begin{document}
\section*{This Sentence No Verb}

Far far away, behind the word mountains, far from the countries 
\emph{Vokalia} and \emph{Consonantia}, there live the blind texts.
Separated they live in Bookmarksgrove right at the coast of the Semantics, 
a large language ocean. The river Duden flows by their place and supplies 
it with the necessary regelialia. It is a paradisemantic country, in which 
roasted parts of sentences fly into your mouth.

Even the all-powerful Pointing has no control about the blind texts;
it is an almost unorthographic life. One day, however, a small line of 
blind text by the name of \textsc{Lorem Ipsum} decided to leave for the far 
World of Grammar. The Big Oxmox advised her not to do so, because there 
were thousands of bad Commas, wild Question Marks and devious Semikoli, but 
the Little Blind Text didn’t listen. She packed her seven versalia, put her 
initial into the belt and made herself on the way. 

When she reached the first hills of the Italic Mountains, she had a last 
view back on the skyline of her hometown Bookmarksgrove, the headline of 
Alphabet Village and the subline of her own road, the Line Lane.

\begin{enumerate}
  \item a
  \item b
\end{enumerate}
\end{document}

enter image description here

You must log in to answer this question.

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