19

I took this line from my thesis style and added it into my document (preceded by \makeatletter) and what it does that whenever I used \ref{figureid} instead of putting just the figure number, it puts "Figure 1".

I can do the same thing for tables, by having \def\p@table{Table~) but what I don't know is what exactly is:

\p@figure
\p@table

I assume they're used internally by ref, but is there anything more (such as \p@(object) is always used as the beginning of something ...)

lockstep
  • 250,273

4 Answers4

19

These are LaTeX kernel macros that are associated with environments. In simple terms anything that is enclosed with a \begin{foo}...\end{foo} is an environment. For example a figure or a table.

Every time you insert a table a counter is incremented. This counter let us call it foo has an associated macro named \p@foo. This macro expands to a printed `reference prefix' of counter foo.

Any \ref to a value created by counter foo will produce the expansion of \p@foo\thefoo when the \label command is executed. \thefoo justs prints the value of counter `foo'.

Change foo to figure or table and it will make more sense.

The \def part defines the macro. You can for example say \def\milan{Milan Ramaiyan} and every time you type \milan it will expand too .. Milan Ramaiyan. The @ symbol is just a special symbol used by TeX and LaTeX to avoid overriding commands accidentally. It needs special treatment and that is why the makeatletter and makeatother are required.

yannisl
  • 117,160
18

In addition to Yiannis fine explanation: you may even extend these \p@ macros such as \p@figure to become more than just a prefix. source2e.pdf gives hints in section 53.2 An extension of counter referencing.

This modification

\makeatletter
\renewcommand*\refstepcounter[1]{\stepcounter{#1}%
\protected@edef\@currentlabel
{\csname p@#1\expandafter\endcsname\csname the#1\endcsname}}
\makeatother

allows you to define \p@figure with an argument, which allows more sophistocated formatting. And it's still backwards compatible to the original version. An arbitrary example:

\renewcommand*{\p@figure}[1]{\emph{figure~(#1)}}

gives see figure (1). This way you may add a prefix and a suffix plus formatting.

Stefan Kottwitz
  • 231,401
  • 2
    I would strongly advise against redefining one of the base latex commands. It can break packages that redefine it, e.g. hyperref. Rather go down to a deeper level, for example to change the reference to a second level enumerated list from 2a to 2(a)
    \makeatletter
    \renewcommand{\p@enumii}{\expandafter\p@@enumii}
    \newcommand{\p@@enumii}[1]{\theenumi(#1)}
    \makeatother
    
    – Danie Els Mar 04 '11 at 08:47
4

A packaged alternative to Stefan's answer is to use the fncylab package, which makes the change he mentions and also defines a \labelformat macro to manipulate \p@counters easily. For example, \labelformat{equation}{(#1)} makes \ref act like \eqref when applied to labels on equations. (I have this activated by default in my documents, actually. The bare number is rarely useful.)

Ryan Reich
  • 37,958
1

I used \usepackage{hyperref} and applied \autoref{tab:minimum-cut-set}. It would generate "Table 1.2" (for instance) what we want to see. However, for algorithm, although "Algorithm" capitalizes but it shows "algorithm 2.1" (for instance).

The caption table, figure are shown properly. Only algorithm occurs the issue as I have mentioned above.

Paulo Cereda
  • 44,220
Tung
  • 291