I'm trying to extend a previous href command (that works) to create a pair of new commands that wrap \hyperlink{}{} and \hypertarget{}{}.

The commands would work together to create hypertargets for hyperlinks to call. (Basically, two way internal PDF links from outline to content are the desired end result - programmatically driven by my Python code )

(Filling in the parameters are no problem, already in my code. But the syntax for the LaTeX command is throwing errors.)

Depending on whether I am in the outline, or in the actual the content, I want to make calls to the commands like so:

\linkfromout{the key text}{the document's Text} 
\linkfromcom{the key text}{The Document's text} 

Correct capitalization may vary for what is printed in the document. But the key text for the \hyperlink and \hypertarget will be all lowercase. The macro needs to prepend com or out to make a distinction for the target location that is reached by the hyperlink call.

The code below, placed in a custom sty file, is failing for me. I've attempted to resolve some errors with the backslash parameter, but it still fails. Any help is appreciated. I'm still new to LaTeX.

\newcommand{linkfromout}[2]{\hyperlink{com \#1}{\hypertarget{out \#1}{\textcolor{linkColor}{\#2}}}}

\newcommand{linkfromcom}[2]{\hyperlink{out \#1}{\hypertarget{com \#1}{\textcolor{linkColor}{\#2}}}}
  • Welcome to TeX.SE.
    – Mico
    Dec 1, 2021 at 0:33

1 Answer 1


I think you just didn't properly escape your new commands (you need a \ before linkfromout etc. in the preamble) and improperly escaped the arguments (no \ before #). The following runs for me without error and is from what I can tell your expected behavior.

\usepackage{hyperref, xcolor}
\newcommand{\linkfromout}[2]{\hyperlink{com #1}{\hypertarget{out #1}{\textcolor{blue}{#2}}}}
\newcommand{\linkfromcom}[2]{\hyperlink{out #1}{\hypertarget{com #1}{\textcolor{blue}{#2}}}}
\linkfromout{the key text}{the document's Text} 
\linkfromcom{the key text}{The Document's text} 
  • thanks @Dan ... that was it. I thought I was going crazy ... now I know for sure, forgetting to backslash the command name itself. LOL
    – RichWalt
    Dec 1, 2021 at 0:42

You must log in to answer this question.

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