3

I'm trying to use the following example from this question to number the paragraphs in my document (slightly modified to remove \thesection. from the paragraph number).

The example:

\documentclass{article}
\usepackage[excludeor]{everyhook}
\usepackage{lipsum}
\newcounter{paragraphs}[section]
\begin{document}
\PushPostHook{par}{%
        \stepcounter{paragraphs}%
        \llap{\theparagraphs\ \kern\parindent}%
}
\section{Foo}
\lipsum
\section{Bar}
\lipsum
\end{document}

Screenshot of the example:

(Colors, shapes, and lines added to illustrate the vertical alignment)

Example screenshot



But I've encountered two problems I don't know how to solve (one is slightly unrelated to the question of this post, but much easier to solve I think):

  1. The number of every paragraph after the first paragraph follows the indentation of those paragraphs. I want the numbers for all paragraphs to be vertically aligned with the number of the first paragraph. Is it possible to set the position of these numbers so that they're all vertically aligned like this? If so, how?

  2. (The unrelated problem): The paragraph numbering seems to include the section title as the first paragraph, so that the first paragraph is labelled 2. I want the first paragraph to start with the label 1. I've tried to use \setcounter{secnumdepth}{4} as stated in some other questions like this one. I've also tried to change the value and put it in different locations in the preamble, but with no luck. I don't fully understand how this command works, so I might just be using it wrong.



Some solutions I've considered:

  1. Using enumitem and setting itemindent to a negative value, but I really do not want to define every paragraph in my document as a list item.

  2. Using something like marginpar to place the paragraph numbers in the margins, but I'm set on using multicols in my document--and apparently it's not possible to use marginpar and multicols together.

  3. Learning how TikZ works and drawing the paragraph numbers in the margins. But I've also read in questions like this one that getting text in margins in a multicols environment is problematic at best.

3

actually I'd use a command \para or some such before each numbered paragraph rather than do this, but if you want to insert the number on a "plain" paragraph that is possible, but I would just locally do this to avoid issues with internal paragraphs in section heads and elsewhere.

here paragraphs inside the paras environment get numbered.

enter image description here

\documentclass{article}

\usepackage{lipsum}
\newcounter{paragraphs}[section]

\newcommand\doparas{%
\setbox8\lastbox% indentation box
\makebox[0pt][r]{\refstepcounter{paragraphs}%
\theparagraphs\qquad}%
\usebox{8}%re-insert indent
}

\newtoks\paraseverypar


\let\parassavedeverypar\everypar
\newenvironment{paras}{%
\paraseverypar\expandafter{\the\everypar}%%
\let\everypar\paraseverypar
\parassavedeverypar{\the\everypar\doparas}}{%
\ignorespacesafterend}

\begin{document}

\section{Foo}
\begin{paras}
\lipsum  
\end{paras}
\section{Bar}
\begin{paras}
\lipsum  
\end{paras}

\end{document}
| improve this answer | |
  • Your answer fixes the indentation problem I've had and also fixes the problem of paragraph numbering. Having paragraphs in a block instead of each being prefixed with a \para is also helpful. I'm checking this answer as correct because it solves both of my problems. Thanks David – Nickolas Peter O'Malley Sep 5 '18 at 13:54
4

I don't really think that the \everypar token list is actually quite the right thing here as LaTeX (and other formats) use paragraphs in many places when they just want to format some text (even if from a huma perspective this is not a "paragraph"). This is why your code shows a number at the heading line as that is handled as a paragraph. Same with, say TOCs lines each would get a number with your code. So as a minimum you need to integrate some flags to turn things on and off.

As for the placement, the most simple way starting from your code would be to place the number before the indentation box, kind of like this:

\PushPostHook{par}{%
        \stepcounter{paragraphs}%
        {\setbox0\lastbox                          % remove indentation if any
         \llap{\theparagraphs\ \kern\parindent}%   % place your number (spacing adlib)
         \box0                                     % reinsert indentation box
        }%                                         % group only to keep box0 local
}

can't test here, but the above should be working modulo typos. To get around numbers in the wrong places you can define a switch \newif ... or ifthen package and integrate that so that the code is only executed if the switch is "true". Of course, setting it on and off is somethign that then has to happen in the document.

| improve this answer | |
  • 1
    ooh we even used the same reinsert indent box, comment obviously group-think:-) (I just noticed the site says you posted half an hour ago, I guess driving in to work means the page doesn't refresh:-) – David Carlisle Sep 4 '18 at 8:17
  • This is perfect Frank, it works exactly as needed. Thank you for the line comments to help me understand what the code is doing. You and David both did exactly what I couldn't figure out how to do: remove and reinsert the paragraph indentation. – Nickolas Peter O'Malley Sep 5 '18 at 13:43

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.