summaryrefslogtreecommitdiff
path: root/Master/texmf-dist/doc/generic/FAQ-en/html/FAQ-labelformat.html
blob: 2f2bbd22c04873b2d48a5337d5cdfba0cf0834a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<head>
<title>UK TeX FAQ -- question label labelformat</title>
</head><body>
<h3>How to change the format of labels</h3>
<p/>By default, when a label is created, it takes on the appearance of the
counter labelled, so the label appears as
<code>\</code><code>the</code><code>&lt;<i>counter</i>&gt;</code> &#8212; what would be used if you
asked to typeset the counter in your text.  This isn&#8217;t always what you
need: for example, if you have nested enumerated lists with the outer
numbered and the inner labelled with letters, one might expect to want
to refer to items in the inner list as &#8220;2(c)&#8221;.   (Remember, you can

<a href="FAQ-enumerate.html">change the structure of list items</a>.)
The change is of course
possible by explicit labelling of the parent and using that label to
construct the typeset result &#8212; something like
<blockquote>
<pre>
\ref{parent-item}(\ref{child-item})
</pre>
</blockquote><p>
which would be both tedious and error-prone.  What&#8217;s more, it would be
undesirable, since you would be constructing a visual representation
which is inflexible (you couldn&#8217;t change all the references to elements
of a list at one fell swoop).
<p/>LaTeX in fact has a label-formatting command built into every label
definition; by default it&#8217;s null, but it&#8217;s available for the user to
program.  For any label &lt;<i>counter</i>&gt; there&#8217;s a LaTeX internal
command <code>\</code><code>p@</code>&lt;<i><code>counter</i>&gt;</code>; for example, a label definition
on an inner list item is supposedly done using the command
<code>\</code><code>p@enumii{<code>\</code><code>theenumii</code>}</code>.  Unfortunately, the internal
workings of this aren&#8217;t quite right, and you need to patch the
<code>\</code><code>refstepcounter</code> command:
<blockquote>

<pre>
\renewcommand*\refstepcounter[1]{\stepcounter{#1}%
  \protected@edef\@currentlabel{%
    \csname p@#1\expandafter\endcsname
      \csname the#1\endcsname
  }%
}
</pre>
</blockquote><p>
With the patch in place you can now, for example, change the labels on
all inner lists by adding the following code in your preamble:
<blockquote>
<pre>
\makeatletter
\renewcommand{\p@enumii}[1]{\theenumi(#1)}
\makeatother
</pre>
</blockquote><p>
This would make the labels for second-level enumerated lists appear as
&#8220;1(a)&#8221; (and so on).  The analogous change works for any counter that
gets used in a <code>\</code><code>label</code> command.
<p/>In fact, the <i>fncylab</i> package does all the above (including
the patch to LaTeX itself).  With the package, the code above is
(actually quite efficiently) rendered by the command:
<blockquote>
<pre>
\labelformat{enumii}{\theenumi(#1)}
</pre>
</blockquote><p>
In fact, the above example, which we can do in several different ways,
has been rendered obsolete by the appearance of the <i>enumitem</i>
package, which is discussed in the answer about 
<a href="FAQ-enumerate.html">decorating enumeration lists</a>.
<dl>
<dt><tt><i>enumitem.sty</i></tt><dd><a href="http://mirror.ctan.org/macros/latex/contrib/enumitem.zip">macros/latex/contrib/enumitem</a> (or <a href="http://mirror.ctan.org/macros/latex/contrib/enumitem/">browse the directory</a>); <a href="http://mirror.ctan.org/help/Catalogue/entries/enumitem.html">catalogue entry</a>
<dt><tt><i>fncylab.sty</i></tt><dd><a href="http://mirror.ctan.org/macros/latex/contrib/fncylab.zip">macros/latex/contrib/fncylab</a> (or <a href="http://mirror.ctan.org/macros/latex/contrib/fncylab/">browse the directory</a>)
</dl>
<p/><p>This question on the Web: <a href="http://www.tex.ac.uk/cgi-bin/texfaq2html?label=labelformat">http://www.tex.ac.uk/cgi-bin/texfaq2html?label=labelformat</a>
</body>