Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
While it seems there should be a sectional counterpart \@makesectionhead to the chapter \@makechapterhead, this is not the case. memoir uses a generic \@startsection macro to initiate the section (at any level below chapter), and subsequently calls \M@sect to build the heading. –  Werner Jul 4 '12 at 20:32
    
That is both helpful to know and slightly mysterious in design. Though chapters heads are "expected" to do potentially more fancy stuff I guess, so maybe it makes sense. –  Lokathor Jul 5 '12 at 0:01
    
@Lokathor This makes sense only if the section titles have a very simple structure: one or two words without any accent. And you still have to remember precisely the title you gave. IMO it's better to stick with the \label method. –  egreg Jul 5 '12 at 15:14

2 Answers 2

up vote 6 down vote accepted

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}
share|improve this answer
1  
I simplified it a bit because I never use the optional argument to the sectioning commands within this document, and it worked out great! \documentclass{memoir} \usepackage[colorlinks=true,linkcolor=blue]{hyperref} \usepackage{letltxmacro} \LetLtxMacro{\oldsection}{\section} \renewcommand{\section}[1]{\oldsection{#1}\index{#1}\label{sec:#1}} \begin{document} \section{Drowning} Test reference \hyperref[sec:Drowning]{Drowning} \end{document} –  Lokathor Jul 5 '12 at 0:00
    
Actually, that seems to have trashed the ability to use the starred versions of the commands ("\section*{}" and so on) under the normal names. You can still use them under the newly aliased names. eg: instead of \subsection*{Foo} you'd use \oldsubsection*{Foo} If you needed to. –  Lokathor Jul 5 '12 at 0:15
    
@Lokathor: Technically you don't need letltxmacro since \section is an argument-less macro within memoir. –  Werner Jul 5 '12 at 6:01
    
you also loose the two extra optional options that memoir provides for all its sections commands. A better choice might have been to use the xparse package to build a feeding chain for \oldsection –  daleif Jul 5 '12 at 11:38
    
What would that feeding chain look like? I'm not at all familiar with the xparse package. –  Lokathor Jul 8 '12 at 23:32

Imho the easiest way to smuggle a \label in is to use \sectionmark:

\documentclass[]{memoir} % Abstand zwischen 2 Absätzen, kein Einrücken (parindent)
\let\orisectionmark\sectionmark
\renewcommand\sectionmark[1]{\label{sec:#1}\orisectionmark{#1}}
\begin{document}
\chapter{Blub}
\section[shortname]{Something very long and perhaps dangerous for a label}
\section{B}
%\section{$\int$} % this breaks!


\ref{sec:shortname}, \ref{sec:B}
\end{document}

But be aware that while a \label accepts quite a lot types of arguments not everything will work.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.