Is there a reasonably easy/effective way to deactivate a hyperlink conditionally using the hyperref package, ie the hyper-target doesn't exist in the LaTeX output ie, display no hyperlink for this specific reference only?

Explanatory Note: Why doesn't the hyper-target exist you ask?

Answer: because the data is input from a database into a predefined latex template with the hyperlink precoded, eg the database code passed to the template includes \hyperlink{XYZ}{ABC} for example - think Word Mailmerge same concept, except we cannot "yet" define from the database if the corresponding hyper-target will be included as well. So in certain circumstances, text details will be inserted into the latex output, but the hyper-target may or may not be included in the document, predicated on what the data selection and sort criteria are, hence the specific referenced target is variable and may not be necessarily included in the Latex Output. However, it is necessary to include the text even though the target is not required for the purpose of the output.

Been reviewing packages and Stackexchange for the past 72 hours for a solution - if it exists but to no avail so far.

Any guidance/pointers would be much appreciated.

Would have supplied an MWE but the code would not be considered an MWE....

ATM everything works fine except that where a missing hyper-target is not included in the compiled output the link shows (as true) however leads the reader to the 1st page in the PDF, obviously because the defined target was not included in the output, so Ideally I would like for the link to appear false, that is not blue.

  • Could you provide a minimal document that shows the final LaTeX file that is compiled and how it's structured? It may help speed solutions your way. For example, are you only using \hyperlink and \hypertarget combinations? Or \hyperref as well? – Werner Jan 17 at 23:59
  • Try this code. It redefines \hyperlink and \hypertarget and uses (La)TeX's \label-\ref system to identify whether there exists a \hypertarget for each \hyperlink. Depending on all the interactions with other packages, this may/may not work. You'll need to compile at least twice with every change in a \hypertarget. – Werner Jan 18 at 0:25
  • @Werner - In this particular issue, we are only using hyperlink and hyper-target, although through the compiled document we use hyperef, varioref and cref as well. I can upload a small compiled PDFif required as an illustration of the issue, but its confidential and I cant attach PDF to the question anyhow, can you suggest how I can post the pdf so you can see it privately? – user204824 Jan 18 at 0:30
  • @Werner - i will give it a go and see if that helps much appreciated! :) – user204824 Jan 18 at 0:34
  • @Werner- Yeah it worked - Thanks - Although its had an effect on our code - an ever so minor effect which I'm sure I can fix no probs - Very much appreciate your time - BTW perhaps someone could suggest this to be added to the hyperref package - as I'm sure I am not the only one who would like conditional links based upon hypertarget being TRUE value – user204824 Jan 18 at 0:54

The following seems sufficient to make \hyperlink{<link>}{<text>} work conditionally based on an accompanying \hypertarget{<link>}{<text>}. It uses (La)TeX's \label-\ref system, writing a label for each <link> within \hypertarget. Then, within \hyperlink, the display is conditioned to either \hyperlink{<link>}{<text>} or <text> if <link> exists or not.



  \label{ht@#1}% Set a \label
  \oldhypertarget{#1}{#2}% Set regular \hypertarget
  \ifcsname r@ht@#1\endcsname
    \oldhyperlink{#1}{#2}% \hypertarget exists
    #2% \hypertarget doesn't exist





| improve this answer | |
  • Just a head up for those interested in this solution. One minor issue with this code - if using varioref and or clevref it deactivates the hyperlinks for those as well. Haven't figure out a resolution for that aspect yet - but working on it, if I find or come up with a solution Ill be sure to post it - admittedly I did not express the inclusion of those packages in my original post - Primarily because we use about 60+ packages in the compiling of our output – user204824 Jan 20 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy