3

For legal documents I need the paragraphs numbered in the format section.paragraph. So e.g. the third paragraph in section 12 would be 12.3. I don't need subsections.

The paragraphs are all indented as a whole. By tweaking code found online I managed to do so. But: when I now try to reference such a paragraph, I get a ref of 4 digits instead of 2.

Expected output for the 3rd paragraph of section 12 (with \label{test} and \ref{test}) is: 12.3 Reality is, unfortunately: 12.3.1.1

Can I somehow cut of the last two digits? I searched online but couldn't find a solution. I looked into \hyperref and it's cousins but didn't see a solution either.

Here is a minimal example:

\documentclass{article}
\usepackage[german]{babel}
\usepackage[a4paper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}

\usepackage{titlesec}
\usepackage{etoolbox}

\setcounter{secnumdepth}{5}
\newcounter{para}[section]
\newcommand{\N}{\noindent\refstepcounter{para}\makebox[\parindent][l]{\thesection.\arabic{para}.}}

\newlength\titleindent
\setlength\titleindent{1.25cm}
\pretocmd{\paragraph}{\stepcounter{subsubsection}}{}{}

\titleformat{\section}{\normalfont\Large\bfseries}{\llap{\parbox{\titleindent}{\thesection\hfill}}}{0em}{}
\titleformat{\subsection}{\normalfont\large\bfseries}{\llap{\parbox{\titleindent}{\thesubsection\hfill}}}{0em}{}
\titleformat{\subsubsection}{\normalfont\normalsize\bfseries}{\llap{\parbox{\titleindent}{\thesubsubsection}}}{0em}{}
\titleformat{\paragraph}[runin]{\normalfont\normalsize}{\llap{\parbox{\titleindent}{\N\hfill}}}{0em}{}

\titlespacing*{\subsubsection}{0pt}{2ex plus 1ex minus 0.2ex}{1.5ex plus .2ex}
\titlespacing*{\paragraph}{0pt}{2ex plus 1ex minus .2ex}{0ex plus 0ex}

\pretocmd{\paragraph}{\stepcounter{subsection}}{}{}
\pretocmd{\subparagraph}{\stepcounter{subsubsection}}{}{}

\begin{document}


\section{Section 1}
\paragraph{}\label{1} Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

\section{Section 2}
\paragraph{} At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. \\
Here is a reference to above paragraph: \ref{1} \\
That's wrong; it should read "1.1"

\end{document}

Above code generates the following output: output of above code

Do I need to reference the refstepcounter?

1
  • 3
    welcome! I'd use an enumerate here for the paragraphs. if you really need a section-like command, just use \subsection and forget the \paragraphs. the names of these divisions are totally arbitrary. all that matters is the hierarchy. if you need two levels, use section and subsection and adjust the formatting however you want. if you need four, use subsubsection and paragraph, too. don't get hung up on the names. \paragraph isn't for paragraph anyway. Commented Aug 30, 2024 at 0:00

3 Answers 3

3

There's no need to go to \paragraph and \subsection is much handier.

Rather than indenting all text you just need to “outdent” the numbers. You'll need to fix the title to take into account the wider left margin.

\documentclass{article}
\usepackage[ngerman]{babel}
\usepackage{geometry}
\usepackage{titlesec}
\usepackage{parskip}

\newlength\titleindent
\setlength\titleindent{1.25cm}

\geometry{
  a4paper,
  top=2cm,
  bottom=2cm,
  left=\dimeval{3cm+\titleindent},
  right=3cm,
  marginparwidth=1.75cm
}

\newcommand{\numberinmargin}[1]{%
  \makebox[0pt][r]{\makebox[\titleindent][l]{#1}}%
}

\titleformat{\section}
  {\normalfont\Large\bfseries}
  {\numberinmargin{\thesection}}
  {0em}
  {}
\titleformat{\subsection}[runin]
  {\normalfont}
  {\numberinmargin{\thesubsection}}
  {0em}
  {}
\titlespacing{\subsection}{0pt}{*1}{0pt}

\newcommand{\para}{\subsection{}}

\begin{document}


\section{Section 1}
\para\label{1} Lorem ipsum dolor sit amet, consetetur sadipscing elitr, 
sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam 
erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et 
ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem 
ipsum dolor sit amet.

\section{Section 2}
\para At vero eos et accusam et justo duo dolores et ea rebum. Stet 
clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

Here is a reference to above paragraph: \ref{1}

That's correct; it should read "1.1"

\end{document}

For the particular document type, you likely want to load parskip.

output

2

Let's take the opportunity to have a look at Latex coding

  • as an experienced user
  • as a novice, using refactoring

If you are an experienced user

Then you know packages titlesec and parskip by heart, and can almost instantaneously write down a solution like Egreg provided. His main aspect is wrt. your question:

  • use the \subsection, you won't use
  • use its implemented counting schemes (i.e. get rid of an explicit counter)
  • "overwrite" the \subsection \newcommand{\para}{\subsection{}}
  • "overformat" it \titleformat{\subsection}[runin]{\normalfont}{\numberinmargin{\thesubsection}}{0em}{}

If you are a novice

Then, starting with the problematic code you posted, I recommend following the process of refactoring, as laid out here, i.e. generalize + clean-up.

With your problematic code in this situation focus on clean-up is required: there is so much code, and probably only a few lines are needed. But which ones?

Turns out, after accomplishing that there's nothing left to generalize, here. Kindly watch my comments preceeding the documentclass. My intention was:

  • making your code shorter and hopefully easier to follow
  • keep my \p approach, which is easy to code in running content
  • and allows counting paragraphs without a leading \p as one counted entity

Turned out that following Egreg's approach wrt. \numberinmargin and using package parskip is wise to adopt. Especially, no formatting of \paragraph elements are needed (you would have to start as in your original post with \paragraph{} text text ... to apply formats from \titleformat{\paragraph}...).

Finally, the code below combines text from your post and from my previous answer.


result

% ~~~ REFACTORING. Focus: brevity + more clarity // main: clean-up ~~~~~~~~~~~
%   Q What code is really neded? => clean-up
%   * replaced \paragraph by \p; reused counters from answer
%   * trying to activate horizontal formats for sections
%   * same for paragraphs // unfinished
%   * removing obsolete lines of code + rearrangements
%   * comment-out all, uncomment step-by-step
%   * PRELIMINARY end: couldn't shift \theparno to the left
%   * \numberinmargin adopted from Egreg's approach
%   * package parskip adopted from Egreg's approach
%   * replace label 1 by A
%   * refactoring ends here: there are no duplicates left to be absorbed

\documentclass{article}
\usepackage[german]{babel}
\usepackage[a4paper,top=2cm,bottom=2cm,left=3cm,right=3cm,marginparwidth=1.75cm]{geometry}

\usepackage{titlesec}
\newlength\titleindent
\setlength\titleindent{1.25cm}

\usepackage{parskip}    % avoids unexpected spaces, see its manual

% ~~~ moving content lefttwards ~~~~~~~~~~~~~~~~~~~~
\newcommand{\numberinmargin}[1]{%
  \makebox[0pt][r]{\makebox[\titleindent][l]{#1}}%
}% 1st box at left margin, 2nd box to the left from there

\titleformat{\section}
    {\normalfont\Large\bfseries}
    {\numberinmargin{\thesection}}
    {0em}
    {}{}

% ~~~ \p related counting ~~~~~~~~~~~~~~
\newcounter{parno}[section]
\newcommand\p[0]{\refstepcounter{parno}\numberinmargin{\theparno}}  
\renewcommand\theparno{\arabic{section}.\arabic{parno}.}

% ~~~ to better locate some labels set ~~~~~~
\newcommand\mylabel[1]{\label{#1} \textbf{(#1)\ }}

% ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}

\section{Section 1}
\p \label{A}Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.

\section{Section 2}
\p At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. \\


Here is a reference to above paragraph: \ref{A} \\
Should be: "1.1"    % <<<


\section{Some thing - from previous answer}
\p Semper Nam Phasellus Curabitur feugiat semper pede convallis sed orci penatibus.  Ullamcorper malesuada Vestibulum Suspendisse nec feugiat sapien sodales In feugiat vitae. Tortor accumsan quis congue Sed pede vitae id tellus mus sapien. Vivamus a urna metus malesuada volutpat morbi Sed tortor.

\p Tristique sit velit vel turpis fermentum vitae condimentum nec ut aliquam. Orci accumsan et eu Phasellus feugiat lorem at Quisque magna pretium. In hendrerit quis Vestibulum Suspendisse id nunc lobortis laoreet Nam senectus. Urna purus ante pede turpis cursus cursus orci pede malesuada.\mylabel{C}

\subsection{test}
\p Quis convallis Donec egestas justo nascetur dui parturient vel urna Aenean. Laoreet pretium tellus ligula lacus quis sem laoreet dapibus laoreet magna. 

Id Integer id id vel semper non ut Nulla molestie.\mylabel{D}

\p Nulla facilisi montes ac interdum dolor urna gravida tincidunt nibh convallis. Hendrerit Quisque convallis lacus Vivamus consequat ut lacinia wisi vel Pellentesque. Sem congue nunc Nullam et porttitor et tellus tellus lacus dolor. 

C: \ref{C}\ 
D: \ref{D}

\end{document}
2
  • 1
    according to your criterion, I'm inexperienced. but my incompetence is not a function of inexperience ;). I wouldn't, personally, advocate one letter macros, even if you are careful to ensure they are not (yet) defined. and, yes, I am far too opinionated. Commented Sep 9, 2024 at 6:23
  • You are right about the too short acronyms. // Don't you have about 1/4 million reasons to prove your statement wrong ;-) ? Thanks for your comments BTW Commented Sep 9, 2024 at 7:37
1

Neglecting all your layout formatting, here's a way to do your reference formatting manually, based on this answer.

Basic steps

Certain structures are counted automatically in Latex, like sections, list items, equations, while paragraphs are not, as you already showed. So:

Remark: In hindsight, this is what you did. However, some of your other things in the preamble seem to spoil it. If I see it correctly, you don't have step (3) below. // Suggestion: try doing less while achieving the same layout result; I'd start with commenting out almost the whole preamble and uncomment step by step, until the compiler stops complaining.

(1) You introduce a new counter parno, which is reset with every new section:

\newcounter{parno}[section]               % new counter, reset with new section

(2) You use a markup \p similar to your \paragraph macro, for increment and outout of \parno's value:

\newcommand\p[0]{\refstepcounter{parno}\theparno\ } % markup counted paragraphs

(3) You reformat \theparno to your preferences, like a section.paragraph style:

\renewcommand\theparno{\arabic{section}.\arabic{parno}} % reformat beginning

(3) You start each new paragraph to be counted by \p; note that non-marked paragraphs aren't counted, so these two just result into "3" in the code below:

\p Dui Vestibulum Aenean libero vestibulum Donec diam lorem rutrum feugiat In. Nullam leo congue sem adipiscing Pellentesque.

Donec hac elit semper Phasellus. Lobortis elit nibh id orci quam In Integer mi lacus Nunc. Non justo vel iaculis Sed at interdum Vestibulum et accumsan eu. 

((5)) Additionally I defined \mylabel, to make the labels set in the example below more evident, by placing a \label and printing it in bold:

\newcommand\mylabel[1]{\label{#1} \textbf{(#1)\ }}

Some alternatives

@cfr's proposal to just use an enumeration list is nice and easy, if you never will use enumerated lists with your legal document.

You may also consider defining your own \newenvironment for paragraphs, if that's useful. E.g. you could hide an enumerated list inside, see previous remark.

With some more experience you may try package refstyle. It's a key-value based approach to modify or extend standard referencing.


result

\documentclass[10pt,a4paper]{article}

% ~~~ https://tex.stackexchange.com/a/653602/245790 
\newcounter{parno}[section]               % new counter, reset with new section
\newcommand\p[0]{\refstepcounter{parno}\theparno\ } % markup counted paragraphs
\renewcommand\theparno{\arabic{section}.\arabic{parno}} % reformat beginning

% ~~~ to better locate the labels set ~~~~~~
\newcommand\mylabel[1]{\label{#1} \textbf{(#1)\ }}

% ~~~ some more space ~~~
\parskip=6pt

% ~~~~~~~~~~~~~~~~~~~~~~~~
\begin{document}
\p Lorem ipsum dolor sit amet consectetuer adipiscing lacus convallis Curabitur nec. Ac mauris Sed ultrices libero \mylabel{A} interdum id vitae metus pede consequat. Sed vel vitae sed Integer Morbi laoreet magnis pretium at laoreet. 

\p Est eget Donec Vivamus justo Nullam ut justo turpis eros dapibus. Mollis pharetra justo habitant quam convallis nulla metus eu congue id. Facilisis et hendrerit eleifend Aenean pellentesque lobortis amet nonummy Nulla ridiculus. 

\p Dui Vestibulum Aenean libero vestibulum Donec diam lorem rutrum feugiat In. Nullam leo congue sem adipiscing Pellentesque.

Donec hac elit semper Phasellus. Lobortis elit nibh id orci quam In Integer mi lacus Nunc. Non justo vel iaculis Sed at interdum Vestibulum et accumsan eu. 

\p Dui Lorem Nam nec congue leo felis cursus massa senectus Fusce. Natoque urna ipsum amet id nibh convallis Lorem ut pellentesque mattis. Id felis lacinia at Phasellus mattis tincidunt Integer metus non pulvinar. At facilisis cursus id laoreet dolor mus \mylabel{B} condimentum Curabitur vitae dui. 

\section{Some thing}
\p Semper Nam Phasellus Curabitur feugiat semper pede convallis sed orci penatibus.  Ullamcorper malesuada Vestibulum Suspendisse nec feugiat sapien sodales In feugiat vitae. Tortor accumsan quis congue Sed pede vitae id tellus mus sapien. Vivamus a urna metus malesuada volutpat morbi Sed tortor.

\p Tristique sit velit vel turpis fermentum vitae condimentum nec ut aliquam. Orci accumsan et eu Phasellus feugiat lorem at Quisque magna pretium. In hendrerit quis Vestibulum Suspendisse id nunc lobortis laoreet Nam senectus. Urna purus ante pede turpis cursus cursus orci pede malesuada.\mylabel{C}

\p Quis convallis Donec egestas justo nascetur dui parturient vel urna Aenean. Laoreet pretium tellus ligula lacus quis sem laoreet dapibus laoreet magna. Id Integer id id vel semper non ut Nulla molestie.\mylabel{D}

\p Nulla facilisi montes ac interdum dolor urna gravida tincidunt nibh convallis. Hendrerit Quisque convallis lacus Vivamus consequat ut lacinia wisi vel Pellentesque. Sem congue nunc Nullam et porttitor et tellus tellus lacus dolor. 

A: \ref{A}\ 
B: \ref{B}\\
C: \ref{C}\ 
D: \ref{D}

\end{document}

P.S.: Even if I'd insert a subsection, the counting scheme doesn't change, as I didn't account for subsections in the style.

withSubsection

1
  • I don't think use of an enumerated environment for this is incompatible with other uses of enumerated environments. of course, you'd need a distinct counter. Commented Sep 9, 2024 at 6:20

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.