# Automatically create labels for sections

I'm working on some kind of experimental law document that will probably be edited a lot. Some law paragraphs make reference to law articles or paragraphs. As the document will be edited a lot, the numbers of these articles and paragraphs will change as well. I'd like to foolproof my document so that it will change the number in the reference when it changes in the document.

Example :

Art. 1 A law I like

§1 Some text §2 Some more text referencing Art. 3 §2

Art. 2 A law I dislike

§1 Ugly text §2 More ugly text

Art. 3 Another law I like

§1 Good text §2 More good text

would become a few months later, after we removed Art. 2 altogether :

Art. 1 A law I like

§1 Some text §2 Some more text referencing Art. 2 §2

Art. 2 Another law I like

§1 Good text §2 More good text

The goal being that the reference changes automatically.

So I thought I could automatically create labels whenever a new article or paragraph is added. Labels that would contain the title of the article, like \label{art:Another law I like} as the title is less likely to change than the number. Then I'd reference to them using nameref.

Here are my articles :

\counterwithout{subsubsection}{subsection}
\counterwithin*{subsubsection}{section}
\setsecnumdepth{subsubsection}
\newcommand{\article}{\subsubsection}

\titleformat{\subsubsection}%command
[block]%shape
{\bfseries}%format
{Art. \arabic{subsubsection}}%label
{1em}%sep
{}%before
[]%after
\titlespacing{\subsubsection}
{0em}{3ex plus 1ex minus .5ex}{10pt}


and my paragraphs :

\newcounter{parnum}[subsubsection]
\renewcommand{\theparnum}{\arabic{parnum}}

\newcommand{\paragraphe}[1]{%
\refstepcounter{parnum}%


Seems like \label{#1} in my \newcommand{\paragraphe}[1] should work but it doesn't. Too long label ? And what about \article ?

Here's a MWE including the structure of my titles:

    \documentclass[a4paper,12pt,british,french,twoside,footnotereset=true]{memoir}
\usepackage{times} % police Times
\usepackage[latin1]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lipsum}
\RequirePackage{titlesec}
\RequirePackage{titleps}

%%% SECTIONS %%%
% [1] sections (I)
\renewcommand{\thesection}{\Roman{section}}
\newcommand{\sectionbreak}{\clearpage}

\titleformat{\section}[frame]
{\normalfont}
{\filright
\footnotesize
\enspace SECTION \thesection\enspace}
{8pt}
{\Large\bfseries\filcenter}

% [2] titres
\setsecnumdepth{subsection}
\newcommand{\titre}{\subsection}

\titleformat{\subsection}%command
[block]%shape
{\bfseries}%format
{TITRE \arabic{subsection} :}%label
{1em}%sep
{}%before
[]%after
\titlespacing{\subsection}
{3em}{3ex plus 1ex minus .5ex}{10pt}

% [3] articles
\counterwithout{subsubsection}{subsection}
\counterwithin*{subsubsection}{section}
\setsecnumdepth{subsubsection}
\newcommand{\article}{\subsubsection}

\titleformat{\subsubsection}%command
[block]%shape
{\bfseries}%format
{Art. \arabic{subsubsection}}%label
{1em}%sep
{}%before
[]%after
\titlespacing{\subsubsection}
{0em}{3ex plus 1ex minus .5ex}{10pt}

% [4] paragraphes
\newcounter{parnum}[subsubsection]
\renewcommand{\theparnum}{\arabic{parnum}}

\newcommand{\paragraphe}[1]{%
\refstepcounter{parnum}%

% [5] sous-paragraphes
\newcounter{subparnum}[parnum]
\renewcommand{\thesubparnum}{\arabic{subparnum}}

\newcommand{\sousparagraphe}[1]{%
\refstepcounter{subparnum}%

%%% DOC %%%

\begin{document}
\mainmatter
\section{Une section}
\titre{Un titre}
\article{Un article}
\lipsum[1]
\paragraphe{Un paragraphe}
\lipsum[1]
\sousparagraphe{Un sous-paragraphe}
\lipsum[1]
\article{Deux articles}

\titre{Deux titres}
\article{Un article}
\lipsum[1]
\article{Deux articles}
\lipsum[1]

\section{Deux sections}
\titre{Un titre}
\article{Un article}
\lipsum[1]
\article{Deux articles}
\lipsum[1]
\paragraphe{Un paragraphe}
\lipsum[1]

\titre{Deux titres}
\article{Un article}
\lipsum[1]
\article{Deux articles}
\lipsum[1]

\section{Trois sections}

\end{document}

• you say \label has not worked but your code does not use \label ? Also why are you not using any of the latex sectioning mechanism rather than just ~\\\indent\normalfont\textbf{ which has no control over page breaks around the heading , and can set the heading as part of the previous paragraph? – David Carlisle Aug 4 '16 at 21:19
• it is best to use simple ascii characters for the argument of \label and \ref while it would be possible to re-use the section title it will fail (without using extra complication in the definitions) if the title uses and accented commands or math or any other non-expandable control sequences. (internally the label argument is used to make a tex command name which has to consist purely of character tokens.) – David Carlisle Aug 4 '16 at 22:04
• Are you aware that there are already specialised classes for typesetting in the legal world? By the way, times is deprecated and ought not be used. Also \RequirePackage should be \usepackage in the document preamble. Do you really use latin1 encoding for your .tex files? Almost all editors now default to utf8. – cfr Aug 5 '16 at 0:08
• I'm very new at Tex still so I mainly took bits of code here and there and tried my hand at it. @DavidCarlisle : I didn't include the part where I used \label as it didn't work. No particular reason I used this specific mechanism for my sections, other than, well, that one seemed to work for my purposes. Are you saying there just isn't a way to do what I'm trying to do for the referencing ? Or just not with \label and \ref ? – Antoine Guenet Aug 6 '16 at 11:54
• @cfr : I found the class br-lex which I used to develop my class. Do you know of others ? Good to know about times, I took that from another class called droit-fr. I don't quite get the difference between \RequirePackage and \usepackage yet, I'll get informed. As for latin1, I use it so that I can simply type in French, with our special characters included (again, I copied three lines that were supposed to be for that purpose but maybe this one is just irrelevant). – Antoine Guenet Aug 6 '16 at 11:57