[Tex/LaTex] “Automatic” section labeling

cross-referencinglinksmemoirsectioning

I would like to be able to link within a document to another section without having to explicitly put a label after that other section command. hyperref does this with the TOC labels, so I think that this isn't an insane desire or anything. I've got a very large document that I'm converting to LaTeX (1100 pages) and much of the conversion has been automated in terms of \section, \subsection, and \subsubsection declarations. But I also need to be able to link to other sections based on their name.

As an example, the text (a game's rulebook) will refer to rules written in another section such as:

Creatures immune to acid's caustic properties might still drown in it if they are
totally immersed (see Drowning).

and then "(see Drowning)" should have the word Drowning be a link to the section (or subsection) titled Drowning.

It's not always clear which other parts will be sections or subsections as I'm going through and adding links (because of the size of the work, I can't remember all of it all the time), so I'd like sections, subsections, and subsubsections to all have the same "prefix" on their label, 'sec:'

Right now I have a command

\newcommand{\skillentry}[2]{\subsection[#1]{#1 #2}\index{#1}\label{skill:#1}}

and then an associated command

\newcommand{\linkskill}[1]{\hyperref[skill:#1]{#1}}

Given that no two sections, subsections, or subsubsections will ever have duplicate names, and also that a whole lot of the document is already written so that I don't want to just use a custom command, It seems like the logical solution would be to redefine \@makesectionhead so that it includes

\label{sec:#1}

inside the \section, \subsection, and \subsubsection command definitions (with #1 here being the name of the section to be created), and a custom shortcut command such as:

\newcommand{\linksec}[1]{\hyperref[sec:#1]{#1}}

then in the appropriate places I just write (see\linksec{Drowning}) and things all connect up properly. Stop me now if this sounds crazy so far.

The catch is that I don't know what the \section command is actually doing. I do have a custom chapter appearance which involves altering \@makechapterhead, so I assume that to change how sections work it would be \@makesectionhead, but I don't know what the default definition of \@makesectionhead is and I can't seem to find it with a google search (probably because I don't know what to search for).

So the question is: What do I write to redefine \section, \subsection, and \subsubsection so that they contain labels as above, but without altering their appearance or anything like that. I'm using the Memoir class if it's relevant (which I think it is). I'm also not using any custom appearance for any of the sectioning commands (though I am for \chapter), because I like the default appearance of them just fine.

Best Answer

I'm not entirely sure I have understood the question, but here goes.

You can \renewcommand the \section, \subsection, etc commands to add the \label of your choosing. The tricky part is that each of these commands take an optional argument (for the headers and toc). Luckily, this has already been tackled in a previous question

"Closed" (square) root symbol

Here's a MWE- if I've misunderstood, please let me know

\documentclass{memoir}

\usepackage{letltxmacro}
\LetLtxMacro{\oldsection}{\section}
\renewcommand{\section}[2][]{\oldsection[#1]{#2}\index{#1}\label{sec:#1}}


\begin{document}

\section[drowning]{Drowning}
Test reference \ref{sec:drowning}
\end{document}