LaTeX forum ⇒ Document ClassesCreate customised references

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

Create customised references

Postby TesTT » Thu Feb 21, 2013 10:35 pm

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:
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?

User avatar
cgnieder
Site Moderator
Posts: 1993
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Thu Feb 21, 2013 11:48 pm

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:

\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

Postby TesTT » Fri Feb 22, 2013 12:00 am

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: 1993
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Fri Feb 22, 2013 12:23 am

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):

\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

Postby TesTT » Fri Feb 22, 2013 12:59 am

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

\newcommand*\savelabel[2]{%
  \immediate\write\@auxout{%
    \noexpand\global\noexpand\@namedef{mylabel@#1}{#2}}}

User avatar
cgnieder
Site Moderator
Posts: 1993
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Sat Feb 23, 2013 6:29 pm

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:

\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


Return to “Document Classes”

Who is online

Users browsing this forum: No registered users and 7 guests