Document ClassesCreate customised references

Information and discussion about specific document classes and how to create your own document classes.
Post Reply
TesTT
Posts: 12
Joined: Wed Aug 15, 2012 6:52 pm

Create customised references

Post by TesTT »

Hey,

My document might look like the text below:
Paragraph 1: Lie groups represent the best-developed theory of continuous symmetry of mathematical objects and structures, which makes them indispensable tools for many parts of contemporary mathematics.
...
As discussed in Paragraph 1 we conclude ...
I would like to use references, i.e. something like this:

Code: Select all

Paragraph 1: Lie groups ... mathematics. \createhiddenlabel{mylabel}{Paragraph 1}
...
As discussed in \ref{mylabel} we conclude ... 
Basically I would like to create reference points which are hidden. Is this possible?

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Create customised references

Post by cgnieder »

Are these »Paragraph 1« like paragraphs actually parts that should be indicated by a sectioning command but should not appear in the table of contents? In that case I would probably use a customized \paragraph and set the counters secnumdepth and tocdepth accordingly. Here is an example:

Code: Select all

\documentclass{article}

\usepackage{lipsum}% dummy text

\setcounter{tocdepth}{3}% subsection level
\setcounter{secnumdepth}{4}% paragraph level

\renewcommand{\theparagraph}{Paragraph~\arabic{paragraph}}
\makeatletter
\renewcommand\paragraph{%
  \@startsection
    {paragraph}% name
    {4}% level
    {\z@}% indent
    {3.25ex \@plus1ex \@minus.2ex}% beforeskip
    {-1em}% afterskip, a negative value creates a run-in heading
    {\normalfont\normalsize\noindent}% style: the only value I have changed
}
\makeatother
\begin{document}

\tableofcontents

\section{Foo Bar}
\paragraph{}\label{par:mylabel} \lipsum[2]

In \ref{par:mylabel} we talked about \ldots

\end{document}
More fine control over the layout for the \paragraph command would be possible through the titletoc package instead of LaTeX's low level command \@startsection that I have used here.

Regards
site moderator & package author
TesTT
Posts: 12
Joined: Wed Aug 15, 2012 6:52 pm

Create customised references

Post by TesTT »

Thanks a lot for that, but basically what I want/need is a simple command like

\CreateAnInvisibleLabel[labelname]{Legendre}

which creates a hidden reference point so that I can access it, e.g.

... as defined in the \ref{labelname} polynomials ...

which gives me

... as defined in the Legendre polynomials ...
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Create customised references

Post by cgnieder »

Well, LaTeX's labels always refer to a counter, i.e. a specific number associated with something like a section or a table caption. The \ref command always only prints the value of the corresponding \the<counter> macro which means you'll get the referenced number. For what you want one would need to define a different label/reference system that writes the words you want to have saved to the aux file and a custom ref command that retrieves it. Maybe this (note that it needs two compilations):

Code: Select all

\documentclass{article}

\makeatletter
% #1: id
% #2: word to print
\newcommand*\savelabel[2]{%
  \immediate\write\@auxout{%
    \noexpand\global\noexpand\@namedef{mylabel@#1}{#2}}%
  #2%
}

% #1: id
\newcommand*\getlabel[1]{%
  \ifcsname mylabel@#1\endcsname
    \@nameuse{mylabel@#1}%
  \else
    ??%
  \fi
}
\makeatother

\begin{document}

Before: \getlabel{labelname}

Use \savelabel{labelname}{Legendre} in the text

After: \getlabel{labelname}

\end{document}
Regards
site moderator & package author
TesTT
Posts: 12
Joined: Wed Aug 15, 2012 6:52 pm

Create customised references

Post by TesTT »

Thanks a lot! I changed \savelabel to the following so that the label is actually hidden (which is what I wanted).

Code: Select all

\newcommand*\savelabel[2]{%
  \immediate\write\@auxout{%
    \noexpand\global\noexpand\@namedef{mylabel@#1}{#2}}}
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Create customised references

Post by cgnieder »

You can also use this with hyperref, by the way. It has the macros \hypertarget{<label>}{<text>} and \hyperlink{<label>}{<text>} that could be used by themselves or included in the above definition:

Code: Select all

\documentclass{article}
\usepackage{hyperref}

\makeatletter
% #1: id
% #2: word to print
\newcommand*\savelabel[2]{%
  \immediate\write\@auxout{%
    \noexpand\global\noexpand\@namedef{mylabel@#1}{#2}}%
  \hypertarget{#1}{}%
}

% #1: id
\newcommand*\getlabel[1]{%
  \ifcsname mylabel@#1\endcsname
    \hyperlink{#1}{\@nameuse{mylabel@#1}}%
  \else
    \textbf{??}%
  \fi
}
\makeatother

\begin{document}

Link before the paragraph: \getlabel{labelname}

Some text about \savelabel{labelname}{lorem ipsum} lorem ipsum dolor \ldots

Link after the paragraph: \getlabel{labelname} and before the next:
\getlabel{another}

Another label\savelabel{another}{another}

\end{document}
Regards
site moderator & package author
Post Reply