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:
  1. Paragraph 1: Lie groups ... mathematics. \createhiddenlabel{mylabel}{Paragraph 1}
  2. ...
  3. As discussed in \ref{mylabel} we conclude ...


Basically I would like to create reference points which are hidden. Is this possible?

Tags:

User avatar
cgnieder
Site Moderator
Posts: 1988
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:

  1. \documentclass{article}
  2.  
  3. \usepackage{lipsum}% dummy text
  4.  
  5. \setcounter{tocdepth}{3}% subsection level
  6. \setcounter{secnumdepth}{4}% paragraph level
  7.  
  8. \renewcommand{\theparagraph}{Paragraph~\arabic{paragraph}}
  9. \makeatletter
  10. \renewcommand\paragraph{%
  11. \@startsection
  12. {paragraph}% name
  13. {4}% level
  14. {\z@}% indent
  15. {3.25ex \@plus1ex \@minus.2ex}% beforeskip
  16. {-1em}% afterskip, a negative value creates a run-in heading
  17. {\normalfont\normalsize\noindent}% style: the only value I have changed
  18. }
  19. \makeatother
  20. \begin{document}
  21.  
  22. \tableofcontents
  23.  
  24. \section{Foo Bar}
  25. \paragraph{}\label{par:mylabel} \lipsum[2]
  26.  
  27. In \ref{par:mylabel} we talked about \ldots
  28.  
  29. \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
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

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

  1. \documentclass{article}
  2.  
  3. \makeatletter
  4. % #1: id
  5. % #2: word to print
  6. \newcommand*\savelabel[2]{%
  7. \immediate\write\@auxout{%
  8. \noexpand\global\noexpand\@namedef{mylabel@#1}{#2}}%
  9. #2%
  10. }
  11.  
  12. % #1: id
  13. \newcommand*\getlabel[1]{%
  14. \ifcsname mylabel@#1\endcsname
  15. \@nameuse{mylabel@#1}%
  16. \else
  17. ??%
  18. \fi
  19. }
  20. \makeatother
  21.  
  22. \begin{document}
  23.  
  24. Before: \getlabel{labelname}
  25.  
  26. Use \savelabel{labelname}{Legendre} in the text
  27.  
  28. After: \getlabel{labelname}
  29.  
  30. \end{document}


Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

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

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

User avatar
cgnieder
Site Moderator
Posts: 1988
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:

  1. \documentclass{article}
  2. \usepackage{hyperref}
  3.  
  4. \makeatletter
  5. % #1: id
  6. % #2: word to print
  7. \newcommand*\savelabel[2]{%
  8. \immediate\write\@auxout{%
  9. \noexpand\global\noexpand\@namedef{mylabel@#1}{#2}}%
  10. \hypertarget{#1}{}%
  11. }
  12.  
  13. % #1: id
  14. \newcommand*\getlabel[1]{%
  15. \ifcsname mylabel@#1\endcsname
  16. \hyperlink{#1}{\@nameuse{mylabel@#1}}%
  17. \else
  18. \textbf{??}%
  19. \fi
  20. }
  21. \makeatother
  22.  
  23. \begin{document}
  24.  
  25. Link before the paragraph: \getlabel{labelname}
  26.  
  27. Some text about \savelabel{labelname}{lorem ipsum} lorem ipsum dolor \ldots
  28.  
  29. Link after the paragraph: \getlabel{labelname} and before the next:
  30. \getlabel{another}
  31.  
  32. Another label\savelabel{another}{another}
  33.  
  34. \end{document}


Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim


Return to “Document Classes”

Who is online

Users browsing this forum: No registered users and 1 guest