Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Is it possible to hyperlink a drawn line to a specific page in the document? I use following command to draw a line: \line(1,0){20} Many thanks! Here is MWE:

\documentclass[a4paper]{article}
\usepackage{hyperref}
\usepackage{polyglossia}

\usepackage{geometry}
\geometry{hmargin={0.2in,0.2in},vmargin={0in,0in},
paperwidth=3.56in, paperheight=5.95in}

\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}
\pagenumbering{gobble}

\begin{document}
\phantomsection \label{PP1}
First page \newpage

\phantomsection \label{PP2}
Second page \newpage

\centering
\line(1,0){20}


\end{document}
share|improve this question
    
Toc might be little complicated. Suppose I labeled a specific page as PP1, now I need a drawn line linked to this page. How do I do it? –  Nina May 21 '12 at 17:21
    
I added MWE. Please ignore TOC. How do you link a drawn line to a page labeled as PP1? –  Nina May 21 '12 at 17:38
    
@ Marco, I understand the work around. However, that doesn't solve my problem, because now the hyperlinked area becomes the size of a font, and placed under a line. I need that line hyperlinked only. –  Nina May 21 '12 at 18:12
    
I found the problem, but no solution ;-). It's XeLaTeX related. With PDFLaTeX and \hyperlink{PP1}{\hbox{\line(1,0){50}}} it works well. –  Marco Daniel May 21 '12 at 18:23
    
show 3 more comments

2 Answers 2

Vairis has shown in his answer, that XeTeX has some problems, if the link does not contain character glyphs. LaTeX's picture environment uses glyphs of a special font for \line, \vector and \circle. This is also the reason for the limitations (only some angles, the sizes are restricted, …). Horizontal and vertical lines are the exception, because they are drawn by rules.

  • Detected by XeTeX: character glyphs.

  • Not detected by XeTeX: rules, images, boxes without glyphs, ...

As solution I have now implemented \XeTeXLinkBox in hyperref 2012/08/18 v6.83b, from README:

\XeTeXLinkBox

When XeTeX generates a link annotation, it does not look at the boxes (as the other drivers), but only at the character glyphs. If there are no glyphs (images, rules, …), then it does not generate a link annotation. Macro \XeTeXLinkBox puts its argument in a box and adds spaces at the lower left and upper right corners. An additional margin can be specified by setting it to the dimen register \XeTeXLinkMargin. The default is 2pt.

Example:

% xelatex
\documentclass{article}
\usepackage{hyperref}
\setlength{\XeTeXLinkMargin}{1pt}
\begin{document}
\section{Hello World}
\newpage
\label{sec:hello}
\hyperref[sec:hello]{%
  \XeTeXLinkBox{\rule{10mm}{10mm}}%
}
\end{document}

Modified example from Vairis:

\documentclass[a4paper]{article}

\usepackage{hyperref}

\usepackage{pifont}

\makeatletter
\@ifpackagelater{hyperref}{2012/08/18}{%
}{%
  \newdimen\XeTeXLinkMargin
  \newsavebox\XeTeXLink@box
  % Space is taken from Zapf Dingbats with zero height and depth
  \font\XeTeXLink@font=pzdr at 1sp\relax
  \newcommand*{\XeTeXLink@space}{%
    \begingroup\XeTeXLink@font\@xxxii\endgroup
  }
  \newcommand{\XeTeXLinkBox}[1]{%
    \begingroup
      \leavevmode
      \sbox0{#1}%
      \begingroup
        \dimen0=\dp0 %
        \advance\dimen0 by \XeTeXLinkMargin
        \setbox2=\llap{%
          \hbox to \XeTeXLinkMargin{%
            \lower\dimen0\hbox{\XeTeXLink@space}%
            \hss
          }%
        }%
        \dp2=\dp0 %
        \ht2=\ht0 %
        \copy2 %
      \endgroup
      \copy0 %
      \dimen0=\ht0 %
      \advance\dimen0 by \XeTeXLinkMargin
      \setbox2=\rlap{%
        \hbox to \XeTeXLinkMargin{%
          \hss
          \raise\dimen0\hbox{\XeTeXLink@space}%
        }%
      }%
      \dp2=\dp0 %
      \ht2=\ht0 %
      \copy2 %
    \endgroup
  }%
}
\makeatother

\setlength{\XeTeXLinkMargin}{2pt}

\begin{document}
\phantomsection \label{PP1}
First page \newpage

\phantomsection \label{PP2}
Second page \newpage

\centering

\hyperref[PP1]{\line(1,1){10}}
\qquad
\hyperref[PP1]{\vector(1,0){10}}
\qquad
\hyperref[PP2]{%
  \XeTeXLinkBox{%
    \begin{picture}(10,10)%
      \put(5,10){\line(0,-1){10}}%
    \end{picture}%
  }%
}   
\qquad
\hyperref[PP2]{%
  \begin{picture}(10,10)%
    \put(5,10){\XeTeXLinkBox{\line(0,-1){10}}}%
  \end{picture}%
}
\qquad
\hyperref[PP1]{\XeTeXLinkBox{\line(1,0){10}}}
\end{document}

Result:

Links with picture objects in XeTeX

The reason for using the space from Zapf Dingbats are:

  • There is a space glyph.
  • Package pifont is already loaded, therefore the fonts are very likely installed.
  • The height and depth of the space glyph is zero.
  • The width is minimized by loading the font at the size of 1sp.
share|improve this answer
add comment

Tried to make your link in different ways:

\documentclass[a4paper]{article}
\usepackage{polyglossia}

\usepackage{geometry}
\geometry{hmargin={0.2in,0.2in},vmargin={0in,0in},
paperwidth=3.56in, paperheight=5.95in}
\usepackage{url}
\usepackage{hyperref}
\hypersetup{
    colorlinks,
    citecolor=black,
    filecolor=black,
    linkcolor=black,
    urlcolor=black
}
\pagenumbering{gobble}

\begin{document}
\phantomsection \label{PP1}

First page \newpage

\phantomsection \label{PP2}
Second page \newpage

\centering
\hyperref[PP1]{\line(1,1){10}} %works

\hyperref[PP1]{\vector(1,0){10}} %works

\hyperref[PP1]{\dotfill} %works

\hyperref[PP2]{\begin{picture}(10,10)\put(0,-1){\line(0,-1){10}} \end{picture}} %not working

\hyperlink{PP1}{\hbox{\line(1,0){10}}} %not working

\end{document}

Got the following output : Different links

With XeLaTeX first three links worked, but the last two did not. (pdfLaTeX does not have this issue)

What I can get out from this is that XeLaTeX does not want to make plain horizontal/vertical line as a link. Maybe because completely horizontal/vertical line is being drawn differently. If you use any real graphical object (for example angled line or vector), then it can easily be used as a link. Hope this will help you to find some kind of a workaround.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.