5

I have a bad habit of not labeling my headlines and I find creating them afterwards rather cumbersome. Is there any script/program that can create a label automatically from the headline, so that

\section{headline1}
\subsection{subsection headline 1}
\section{headline2}

becomes

\section{headline1} \label{sec:headline1}
\subsection{subsection headline 1} \label{sec:subsection headline 1}
\section{headline2} \label{sec:headline2}

In general, a macro that copies the headline, from the \XXXsection{} command into a \label{} command?

If there is not, would it be possible to make something like that, and how would that be done? It does not have to be a part of a tex editor, maybe something that just looks at the .tex file as a .txt?

Edit: The headlines will in my case only contain a few words. Besides this there will only be non-formatted text, and no special sign.

Werner
  • 603,163
Mikkel
  • 187
  • The problem with your hope is that labels must be given names, and unless those names are meaningful to the author in some fashion, they are quite useless. So one could, perhaps, create a \label{one} for section 1, etc. but what good would that do? – Steven B. Segletes May 21 '15 at 11:31
  • You could operate on your TeX file with something like an awk script to accomplish this kind of thing, but in most cases you want your labels to be shorthand so they are easy to refer to. I assume you know that you don't need the label at all unless you plan to \ref the section? For example, the section will appear in your table of contents without the label. – James May 21 '15 at 11:46
  • Creating a label "sec:headline1" from \section{headline1} is easy. But what if the title contains math, non-ASCII-chars, commands line \emph, quotes? What if you have two sections with the same name? And what if you decide to change the title to newheadline1? Do you want to adapt all references? I create a label when I add the first reference. In the long run it is easier. – Ulrike Fischer May 21 '15 at 12:13
  • @UlrikeFischer The headlines does not contain math, due my supervisor that are to be kept within a few words, and the labes that I create myself are normally the exact same as headline for the mentioned reasons. – Mikkel May 21 '15 at 16:44
  • @James The label can be the exact headline. – Mikkel May 21 '15 at 16:46
  • @StevenB.Segletes All labels should just be the exact headline, though that was clear from the example. How would I make that more clear than it is? – Mikkel May 21 '15 at 16:47
  • "Normally" are not 100%. How can you be absolutly sure that your headline will never contain anything that would break in a label command? No non-ascii-char, no font command? And that you will never change one headline? – Ulrike Fischer May 21 '15 at 17:03
  • It is clear what you said, but what you call the headline is typically a multi-word phrase, and can include macros like italics, math content, greek letters, etc. None of those (multiword, macros, mathmode,etc) are remotely acceptable in a label name. For example, what label would you hope to create with \section{The meaning of $\pi$}? And even if you could create such a label, would you really want to type \ref{The meaning of $pi$} just to get the number, for example, "3"? – Steven B. Segletes May 21 '15 at 17:20
  • @UlrikeFischer Sorry, I did not make that clear. Due to the format that I follow, my headlines can only contain non formatted text. no special signs. In that case that you change a headline you would have to change the label too.

    But my question concerns if it would be possible to auto create a label from a headline. not keep the label updated to match the headline.

    – Mikkel May 21 '15 at 18:46
  • @StevenB.Segletes As explained above, the headlines can only contain plain non-formatted text.

    To the part with writing the full label, I can only say that I, at least, uses an editor that have auto complete, so that I only need to write the first couple of letters.

    – Mikkel May 21 '15 at 18:49

1 Answers1

4

If you're not using any sectional packages, then you can patch \@sect - used by all (unstarred) sectional commands to set the heading:

enter image description here

\documentclass{article}
\usepackage{etoolbox}

\makeatletter
\patchcmd{\@sect}% <cmd>
  {\@xsect}% <search>
  {\label{sec:#8}\@xsect}% <replace>
  {}{}% <successs><failure>
\makeatother
\begin{document}

\section{headline1}
\subsection{subsection headline 1}
\section{headline2}

Section~\ref{sec:headline1}; 
Subsection~\ref{sec:subsection headline 1}; 
Section~\ref{sec:headline2}

\end{document}

I would not encourage this, as \labels should use descriptive references that may be completely unrelated to the actual title/heading.

The above patch works with hyperref assuming that it's loaded after the \patchcmd.

Werner
  • 603,163