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.

8
  • I don't get this really. You want replace \hyperref to fall back to \ref?
    – user31729
    Aug 30, 2016 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, 2016 at 12:48
  • 1
    Why don't you do simply \providecommand{\hyperref}{}? Aug 30, 2016 at 13:04
  • 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, 2016 at 15:16
  • 1
    @KeksDose: Mine includes the possibility that you specified an optional argument with \hyperref[.]{..}.
    – Werner
    Aug 30, 2016 at 16:31

2 Answers 2

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.

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}
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .