5

In my .tex file I added a hyperref link in my footer, which takes you back to the Table of contents. I want this link to disappear when I let the user print my document. I thought the OCG-P package would be my ideal solution for this, but I do not get the link to disappear when printing. Does anybody have an idea on how to fix this? I have provided an MWE below. Thanks in advance!

\documentclass{article}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{datapie}
\usepackage{fancyhdr}
\usepackage{ocg-p,hyperref}
\usepackage{lipsum}

\pagestyle{fancy}
\fancyhf{}

\renewcommand{\contentsname}{Summary}

\patchcmd\tableofcontents{\contentsname}{\hypertarget{TOC}{\contentsname}}{}{\failed}

\begin{ocg}[printocg=never]{Footer2}{id1}{0}
\lfoot{\hyperlink{TOC}{$\leftarrow$ Summary}}
\end{ocg}

\begin{document}

\begin{titlepage}
    \newpage
    \tableofcontents
    \thispagestyle{empty}
    \newpage
\end{titlepage}

\section{SECTION 1}
\lipsum
\end{document}

2 Answers2

4

The ocg environment must be part of the footer definition. Put it inside the \lfoot{...} argument:

\documentclass{article}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{datapie}
\usepackage{fancyhdr}
\usepackage{ocgx2,hyperref}
\usepackage{lipsum}

\pagestyle{fancy}
\fancyhf{}

\renewcommand{\contentsname}{Summary}

\patchcmd\tableofcontents{\contentsname}{\hypertarget{TOC}{\contentsname}}{}{\failed}

\lfoot{\begin{ocg}[printocg=never]{Footer2}{id1}{1}%
\hyperlink{TOC}{$\leftarrow$ Summary}%
\end{ocg}}

\begin{document}

\begin{titlepage}
    \newpage
    \tableofcontents
    \thispagestyle{empty}
    \newpage
\end{titlepage}

\section{SECTION 1}
\lipsum
\end{document}
AlexG
  • 54,894
  • 1
    This is still not working in my case. Could it have something to do with the pdf viewer I'm using? It is Document Viewer by Linux. – Raphael Spaans Jul 24 '17 at 08:18
  • 2
    @RaphaelSpaans : Yes, this depends on the viewer. To my knowledge, only Acrobat Reader, Foxit (Windows) and the built-in PDF viewers of Firefox and Chrome support the OCG settings for printing. – AlexG Jul 24 '17 at 08:23
2

I ran into the same problem and the solution was that I needed to run pdflatex twice. It was only after the second run that the output PDF had the areas hidden correctly when printing.

  • This won't help with the document source as given in the question. As long as the ocg environment is not moved into the footer definition, the OCG-related PDF code won't make it into the PDF output. – AlexG Aug 30 '17 at 08:57
  • True. My answer was more because this question comes up first on Google, so it was more to help out others experiencing the same problem, but with a different underlying cause. – Malvineous Aug 31 '17 at 07:31