Text FormattingHow do I write a custom reference command?

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
henrikandresen
Posts: 5
Joined: Wed Nov 04, 2009 4:32 pm

How do I write a custom reference command?

Post by henrikandresen »

Hello All

I am trying to rewrite a reference command.

Currently I use the current latex code

Code: Select all

\section{Theory - ABC}\label{sec:theory_abc}
Text etc.
In \textit{ Section \ref{sec:theory_abc}: Theory - ABC}
I would like to make a \newcommand{secref} that would output this automatically, so I only needed to write

Code: Select all

\secref{sec:theory_abc}
, and then it would write Section and the section name as well.

In addition, I am using the hyperref package, so I would really like if the full text could become a link, but I have no idea how to do this.

All help is appreciated

Cheers

Henrik Andresen
Last edited by henrikandresen on Mon Nov 09, 2009 4:50 pm, edited 1 time in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
magicmoose
Posts: 90
Joined: Fri Nov 06, 2009 7:29 am

How do I write a custom reference command?

Post by magicmoose »

Hi Henrik,

Try this, I think it should what you are asking (except only the number will be part of the hyperlink)

Code: Select all

\newcommand{\secref}[1]{Section \ref{#1}}
Maybe someone with some more knowledge may be able to help out with making the whole word part of the link as well, but that should get you going anyway

Hope that helps
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

How do I write a custom reference command?

Post by gmedina »

Hi Henrik,

you could use some of the features offered by the cleveref package. Take a look at the following example:

Code: Select all

\documentclass{article}
\usepackage[breaklinks=true]{hyperref}
\usepackage{cleveref}

\newcommand*\secname{}
\crefformat{section}{#2\textit{section~#1: \secname}#3}
\Crefformat{section}{#2\textit{Section~#1: \secname}#3}

\begin{document}

\section{Test section}\label{sec:test}\renewcommand\secname{Test section}

We discussed in \cref{sec:test}...

\Cref{sec:test} contains a ...

\end{document}
Remarks: 1) For each new section you will have to use

Code: Select all

\renewcommand\secname{<name>}
2) This approach doesn't cooperate with the backref option of hyperref and might fail if you are using the memoir document class (both facts mentioned in the package documentation.)
1,1,2,3,5,8,13,21,34,55,89,144,233,...
henrikandresen
Posts: 5
Joined: Wed Nov 04, 2009 4:32 pm

How do I write a custom reference command?

Post by henrikandresen »

Thank both of you. I have made a mix of the two. I found a command in the hyperref package called \nameref and combined it with your suggestions. What I have gotten out of it is the following command

Code: Select all

\newcommand{\secref}[1]{\hyperref[#1]{\textit{section \ref{#1}: \nameref{#1}}}}
\newcommand{\tabref}[1]{\hyperref[#1]{\textit{table \ref{#1}: \nameref{#1}}}}
\newcommand{\imgref}[1]{\hyperref[#1]{\textit{image \ref{#1}: \nameref{#1}}}}
\newcommand{\appref}[1]{\hyperref[#1]{\textit{appendix \ref{#1}: \nameref{#1}}}}
Which I can call like the \ref command. The entire text becomes italic and is a link.

Thank you

Henrik
Post Reply