4

I'm using a custom command and environment I found on this site for numbering paragraphs. I have my document set up the way I like it, except for the spacing after the number.

I'd like to have the spacing from the number to the beginning of the paragraph to be equal for both single- and double-digit paragraph numbers.

As it stands, the beginning of the paragraph starts a little further right on paragraphs with a double-digit number than it does with a single-digit number.

How can I evenly space them out, so each paragraph starts an equal distance from the margin, while maintaining some space between the number and the paragraph like in this example?

\documentclass{article}
\usepackage{lipsum}

\setlength\parindent{0cm}

\newcommand{\pn}{\bfseries\arabic{pc}}
\newcounter{pc}
\newenvironment{pns}{
    \par
    \everypar{\noindent\stepcounter{pc}\leavevmode\textsuperscript{\pn\hspace{1.5em}}}
    \setcounter{pc}{0}
}{}

\begin{document}
    \begin{pns}
        this is a paragraph

        this is a paragraph

        this is a paragraph

        this is a paragraph

        this is a paragraph

        this is a paragraph

        this is a paragraph

        this is a paragraph

        this is a paragraph

        this is a paragraph

        this is a paragraph

        this is a paragraph
    \end{pns}
\end{document}

Here's a picture of what it looks like now: enter image description here

  • 1
    What should the alignment be like when this is a paragraph spans more than one line? – Werner Oct 24 '14 at 3:52
  • I removed superscript, added color, and added more text to show you. It should look like this: i.imgur.com/GBoyfLk.png Although, to be honest, it would be interesting to see how it would look if the remaining lines lined up with the indent of the first line. I'm just not sure how to set that up. I'd like to see what both looks like. – Nickolas Peter O'Malley Oct 24 '14 at 3:58
4

Note after edits:

This answer now demonstrates

  1. how to align the paragraph numbers using a numerical test and \hphantom;
  2. the next section (first edit) trims the code and demonstrates its application to longer texts;
  3. finally it shows a slightly different method to put the notes in the outside margins of a two-column layout, using the memoir class.

All you have to do is test the value of pc using \ifnum; if it is less than 10, then leave an extra blank space using \hphantom{0}; otherwise, don't do anything (\relax); then print the value of counter pc.

You could add the phantom space on either side of the numeral, of course.

\newcommand{\pn}{%
    \bfseries%
    \ifnum\value{pc} < 10 \hphantom{0}%
    \else\relax\fi%
    \thepc%
}

enter image description here

EDIT

Here's a leaner version using dummy text. I may be wrong but I don't think you need all that code in the pns environment, at least I don't see any difference without it. If this is for the whole document, you might use AtBeginDocument so you don't even need that environment.

I noticed that LaTeX defines a \quad skip as \hskip 1em\relax, so I replaced your \hspace command with one based on this.

\documentclass{article}
\usepackage{lipsum}

\setlength\parindent{0cm}

\newcounter{pc}
\newcommand{\pn}{%
    \textbf{\thepc}%
    \ifnum\value{pc} < 10 \hphantom{0}%
    \else\relax\fi%
    \hskip 1.5em\relax%
}
\newenvironment{pns}{%
    \everypar{\noindent\stepcounter{pc}\pn}%
}{}

\begin{document}
\begin{pns}
\lipsum[1-15]
\end{pns}
\end{document}

enter image description here

EDIT 2

You can put the numbers in the outside margins of a two-column layout using the memoir class.

\documentclass[twocolumn]{memoir}
\usepackage{lipsum}

\setlrmarginsandblock{1.5in}{1in}{*}
\setmarginnotes{1.5em}{4em}{\onelineskip}
\marginparmargin{inner}
\setmpjustification{\raggedleft}{\raggedright}
\checkandfixthelayout

\newcounter{pc}
\newcommand{\pn}{%
    \marginpar{\mpjustification\textbf{\thepc}}%
}
\newenvironment{pns}{%
    \everypar{\noindent\stepcounter{pc}\pn}%
}{}

\begin{document}
\begin{pns}
\lipsum[1-15]
\end{pns}
\end{document}

enter image description here

| improve this answer | |
  • I'm getting a lot of text "wobbling" when I try this in the actual document. The numbers don't line up, but there seems to be some space before some of the numbers, and it happens randomly. What might cause this? – Nickolas Peter O'Malley Oct 24 '14 at 4:16
  • Adding a second comment because I ran out of time to edit. It seems like the problem goes away when I re-enabled superscript. Thanks. Should I open a second question for an answer to lining up the remaining lines of the paragraph with the indentation of the first line? – Nickolas Peter O'Malley Oct 24 '14 at 4:22
  • 1
    @NickolasPeterO'Malley I don't see the wobbling, but I added a second version in hopes it might clear something up. For the second question, there is information on this site and elsewhere about creating hanging indents, but perhaps you might want to move the paragraph numbers into the left margin instead. – musarithmia Oct 24 '14 at 4:40
  • The wobbling stopped, thanks. I'll check out hanging indents. I would actually love to have the paragraph numbers in the margin, but the document I'm writing has two columns. I've tried getting text in the margin (left margin for left column, right for right column) but I couldn't get it to work. – Nickolas Peter O'Malley Oct 24 '14 at 5:03
  • @NickolasPeterO'Malley The last edit shows how to do the two-column setup with memoir. – musarithmia Oct 24 '14 at 16:03

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.