8

In a (complicated) definition of a command, I'm using \hyperref{.... But the package hyperref slows down compilation. So I'd like to make \hyperrefsimply do nothing, as long as the package hyperref is not loaded.

In a later stage I'd like to add \usepackage{hyperref}to my text, when I have to compile less often.

So I thought of \providecommand. What I don't know, is how to define \hyperref in a way that it simply does nothing, but without preventing the rest of my complicated command from being executed.

I wrote a MWE. In case the hyperref package is not loaded, \secrefshould be executed as if there were no \hyperref, which means in the case of my stupid example, just \refshould be executed.

\documentclass{article}

\providecommand{\hyperref}{»DO NOTHING«}

\newcommand{\secref}[1]{%
  \hyperref{%
    \ref{#1}
  }
}

\begin{document}

\section{Huhu}
\label{CLA:huhu}

Text \secref{CLA:huhu}

\end{document}

I know that my example won't work, if I load the hyperref package, but this isn't the question. I lacked of a better idea for the MWE.

Keks Dose
  • 30,892
  • I don't get this really. You want replace \hyperref to fall back to \ref? –  Aug 30 '16 at 12:45
  • No, I want \hyperrefto do nothing, as long as the package hyperref is not loaded. But 'nothing' means, the rest of the command \secrefshould be executed. – Keks Dose Aug 30 '16 at 12:48
  • 1
    Why don't you do simply \providecommand{\hyperref}{}? – Ulrike Fischer Aug 30 '16 at 13:04
  • @UlrikeFischer Because I'm clueless?! – Keks Dose Aug 30 '16 at 13:07
  • \hyperref either has an optional argument and a mandatory one or another version with 4 mand. arguments, none of them is gobbled away with \providecommand{hyperref}{} in my point of view! –  Aug 30 '16 at 13:38
  • 1
    @KeksDose: You have a spurious space in your definition of \secref. If you remove this space, then \providecommand{\hyperref}[2][]{#2} should do what you ask. – Werner Aug 30 '16 at 15:16
  • @Werner What is the difference to Ulrike's idea \providecommand{\hyperref}{}? – Keks Dose Aug 30 '16 at 16:25
  • 1
    @KeksDose: Mine includes the possibility that you specified an optional argument with \hyperref[.]{..}. – Werner Aug 30 '16 at 16:31

2 Answers2

9

The hyperref bundle comes with the nohyperref package, which provides the command syntax of hyperref without the actual complicated hyperref mechanism. This makes it useful for draft documents, which seems to be what you're looking for.

Example:

\documentclass{article}

\usepackage{nohyperref}

\newcommand{\secref}[1]{%
  \hyperref[#1]{section \ref*{#1}}%
}

\begin{document}

\section{Huhu}
\label{CLA:huhu}

Text \secref{CLA:huhu}

\end{document}

When you're approaching the final version of the document, you just need to replace nohyperref with hyperref.

Another possibility is to load hyperref with the draft option:

\documentclass{article}

\usepackage[draft]{hyperref}

\newcommand{\secref}[1]{%
  \hyperref[#1]{section \ref*{#1}}%
}

\begin{document}

\section{Huhu}
\label{CLA:huhu}

Text \secref{CLA:huhu}

\end{document}

This also speeds up document compilation.

Nicola Talbot
  • 41,153
2

You may simply use \ignorespaces:

\documentclass{article}

\providecommand{\hyperref}[4]{#1\ignorespaces}

\newcommand{\secref}[1]{%
  \hyperref{%
    \ref{#1}
  }{}{}{}
}

\begin{document}

\section{Huhu}
\label{CLA:huhu}

Text \secref{CLA:huhu}


\end{document}
CarLaTeX
  • 62,716