2

I am searching for a way to have a command, say \AP (anchor point), that puts a \phantomsection (or anything else) at the left of the current position, say 1 cm to the left of the current column.

The reason for that is that when a reference is used (on a pdf viewer executed with a small screen or a high zoom), a \phantomsection located in the middle of the line yields jumps to location inside the column, for which only part of the column is visible. The proper location would be to have the viewer have the upper left corner of the screen at the left of the column.

It is in some sort a very simple variant of marginpar but not involving the floating mechanisms.

2 Answers2

3

I am no expert on phantom sections, but perhaps this use of the tabto package might suffice. Here, I define \Phantom to move to a place 1cm to the left of the margin before issuing the \phantomsection and returning to its former place on the line.

\documentclass{article}
\usepackage{hyperref}
\usepackage[nopar]{lipsum}
\usepackage{tabto}
\newcommand\Phantom{\tabto*{-1cm}\phantomsection\tabto{\TabPrevPos}}
\begin{document}
\tableofcontents

\hrulefill

\lipsum[4]
\Phantom
\addcontentsline{toc}{section}{Some place in the document}
\label{some}
This is just \hyperref[some]{some place} in the document.

\lipsum[1-12]
\Phantom
\addcontentsline{toc}{section}{Other place in the document}
\label{other}
This is just \hyperref[other]{other place} in the document.

\end{document}

Note: there is the one area where this approach can cause an issue, when \Phantom is invoked near the end of a line. This tab location, which is restored by \TabPrevPos, occurs before TeX's paragraph aligning algorithm occurs. Therefore, if \Phantom is invoked near the end of a line, there could occur issues of margin overrun of subsequent text out the right margin.

  • 1
    Thanks a lot! This is a very good answer that achieves much better thing that my own attempts ever did!

    Nevertheless this is not as perfect as possible because:

    • Indeed \Phantom at the end of a line inserts extra spaces, which makes it unusable in a systematic way as you mentionned in your post,
    • I have the impression that this solution performs a \leavevmode which means inserting extra lines if used just before a sectionning command.

    So I am still searching for a solution to this problem.

    – Thomas Colcombet Jan 10 '17 at 14:12
  • @TTC Yes, the very first thing \tabto does is \leavevmode (see tabto.sty in http://ctan.org/tex-archive/macros/latex/contrib/tabto) – Steven B. Segletes Jan 10 '17 at 14:17
  • @StevenBSegletes I am not too much worried about the this vmode issue, but more about the end-of-line problem. – Thomas Colcombet Jan 10 '17 at 15:38
1

With pdflatex you can use the pre extension of \vadjust to insert the \phantomsection just before the current line of the vertical list. Here shown in an example with two columns:

\documentclass[twocolumn]{article}

\usepackage{hyperref}
\usepackage{blindtext}

\begin{document}
\section{Test}
Here\vadjust pre {\phantomsection}\label{test:a} \blindtext
\Blindtext[2]

And somewhere in this line of text
here.\vadjust pre {\phantomsection}\label{test:b} 
\blindtext

\Blindtext

Goto first \ref{test:a} or second \ref{test:b}.
\end{document}
Schweinebacke
  • 26,336
  • That is excellent! (I am just a bit sad to rely on pfdtex...)

    I am still wondering about a point, which is not really related to the solution.

    I came up with the code \newcommand\Phantom{\vadjust pre{\smash{\phantomsection x}}}. Here xis just a debugging marker (say, in draft mode) to visualize where the \phantomsection was put. When this code is used on the first line of a page, then it inserts an extra top line, and all the text is shifted of one line down. Is there a solution to make such code really invisible?

    – Thomas Colcombet Jan 10 '17 at 15:14
  • Using \vadjust in vertical mode is not so good. Try \leavevmode before \vadjust. Don't know whether or not it helps. Maybe you have to test for vertical mode and do some skips in that case. – Schweinebacke Jan 10 '17 at 15:25
  • \leavevmode does not solve this problem. It occurs when the code is used in the a line (so in horizontal mode), but the first line of the page. – Thomas Colcombet Jan 10 '17 at 15:28
  • @Thomas: If you want to output text use package marginnote. Without text output but only \phantomsection the problem does not occure. – Schweinebacke Jan 10 '17 at 15:53
  • The thing is that margin notes 'take space' in the margin. After further searching, I have found this answer of egreg about putting markers in the margin, and it seems to do exactly what I want (and is not specific to pdflatex): stack exchange – Thomas Colcombet Jan 13 '17 at 08:45