# Did anybody succeed in opening a text file at a certain line from a PDF link?

In my attempt to clarify some inconclusive topics and a part of the series "did anybody succeed" (e.g. Did anyone succeed in playing embedded movies inside Acrobat in Linux?), here it is a new question:

Did anybody, with a certain combination of PDF viewer and editor in any system (e.g. GNU/Linux), succeeded in creating and opening links in the PDF that open a text file at a certain line?

What exact combination of editor (e.g. gedit, emacs, vim) and viewer (e.g. evince, acrobat) and settings (e.g. security settings in acrobat) did you use?

My current strategy is to use the command

\href{run:textfile.txt}{Link to text file (line 20)}


of course, that doesn't bring the cursor to the correct line, I have to search for the line manually. (It does this both in Evince and Acrobat after confirmation).

The question, is how to do the same but opening a certain line in the text. My failed attempt is to

\href{run:/usr/bin/gedit +20 textfile.txt}{Link to text file (line 20)}


by trying to exploit the command line option that allows to open the file in a certain line. The fact is that this doesn't even work because it is not recognized as command line, (in evince, "Error when getting information for file '/usr/bin/gedit +20 textfile.txt': No such file or directory." , in acrobat: "Could not open the file '/usr/bin/gedit +20 textfile.txt'"

I even though about hijacking the "inverse search" feature that some viewers/editors have to make it link back to any arbitrary line or file from a position in the PDF), but I don't know where to begin (for now, I don't care how involved the code is for the solution, as long as it works with an existing viewer and editor)

(For illustration, I am using Acrobat 9.4.1 in GNU/Linux, Evince 3.8.3, and gedit 3.8.3)

(Some previous inconclusive answers: https://tex.stackexchange.com/a/2982/1871 and https://tex.stackexchange.com/a/123982/1871)

Edit: Clarification: I don't want to link to the TeX source but to an arbitrary text file.

Edit 2: Doing some research, I found that, in principle, Javascript can execute an external program (perhaps after disabling some security wall), and Javascript can be embedded in the PDF. Could this be the answer? (at least for Acrobat viewer)

• I managed to achieve this using Emacs as an editor and Skim as a viewer. The relevant part of that configuration is also in the question tex.stackexchange.com/questions/11613/…. – Ricardo Sep 11 '13 at 20:24
• @Ricardo, it looks like this is a solution for the opposite, that is, to open a PDF from the Editor. – alfC Sep 11 '13 at 20:30
• you are correct and I misread the question as opening the PDF at a certain line. Sorry for getting your hopes up =(. – Ricardo Sep 11 '13 at 20:35
• The PDF specification contains references/anchors for pages and has a predefined format. Text files on the other hand don't. I would say this is heavily dependent on the text file viewer you use, making an inherent "jump to line X" functionality difficult to implement. – Werner Sep 11 '13 at 23:54
• @Werner, true, must be difficult to implement, that is why I was relying on the command line to open the text file, because it seems that the '+number' argument is quite standard (works in vi, emacs and gedit). However on the same lines synctex (I don't know its internals) precisely has precisely this feature (although limited to the source file.) – alfC Sep 12 '13 at 2:10

To launch from PDF the editor with an unrelated text file with specific options, probably you need do this indirectly.

1. Create a script that launches all what you want (e.g. containing gedit +42 file.txt). Depending on the viewer file.txt may need to be an absolute path.
2. Link from TeX to that script (e.g. with href)
3. The script extension (for example .sh or .mygedit, rungeditfromline42.sh or rungeditfromline42.mygedit) must be among the executable mime types.

Depending on the viewer, one (or both) of the following points need to be set:

1. (Evince, Acroread) The extension has to be associated with some type of execution of the script. One way I found for this is here (replacing Java with Bash): https://ask.fedoraproject.org/en/question/9735/how-do-i-launch-jar-files-using-nautilus/?answer=15275#post-id-15275 (I don't know of an automatic way of doing this).
2. (xpdf) Make the script executable chmod +x rungeditfromline42.sh.
3. (No solution yet for Okular)

All that so that acroread knows how to handle (in other cases, will be treated as any normal text). I also noticed that, for some reason, evince requires the full paths (inside the scripts).

I tested this in Fedora 20. A similar problem is solved in http://latex-beamer-class.10966.n7.nabble.com/run-a-shell-script-from-a-beamer-generated-pdf-td2049.html. But the instruccions are not complete in my opinion.

EDIT 2015: How to make script extension to be associated with some type of execution of the script? (point 4 above)

(If this is step is not taken Acrobat or Evince might open the script file and not execute the script)

In ~/.local/share/applications/ create a file called run-gedit.desktop:

[Desktop Entry]
Encoding=UTF-8
Type=Application

Exec=bash %f
Icon=icon

Name=run-gedit
Comment=Run the gedit file


From nautilus right-click on the rungeditfromline42.mygedit file (see above) and click the "Open With" tab, scroll down until you find "run-gedit" then click it and "Set as default". Go to Evince, click in the link to test that the script is run.

A loosely related note about SyncTeX

Since 2008 is possible to use SyncTeX to allow synchronisation between the source and PDF files.

To enable synchronisation, SyncTeX creates an auxiliary file in the format filename.synctex.gz.

Several LaTeX editors use SyncTeX by default. You do not need change the source .tex file at all. For example in TeXWorksyou only need one rigth mouse click in the PDF viewer to open a menu and then choose go to the source code (to the lines that correspond to that is actually cliked in the PDF viewer), and with same procedure in the text editor you can return to the PDF viewer (showing the page that you are actually editing).

• I didn't mentioned that I wanted to go back to the source text file, I want to open an arbitrary text file. (a hack solution may use synctex but that was not the intent) – alfC Sep 11 '13 at 22:41
• @alfC, sorry I assumed that this was the unique objective, as launch programs from a PDF is usually not wanted because security issues. – Fran Sep 11 '13 at 23:59
• ok, so apparently for each (non trivial) executable link one has to create a file that goes along the PDF. Not very convenient but finally it works.I edited your answer to stress the main points. – alfC Sep 24 '13 at 8:26
• I am confused, this doesn't work anymore. I'll remove the accepted to avoid confusion and put it back when I found the solution or someone else can confirm that this works in his/her system. – alfC Nov 22 '14 at 0:17
• @alfC No, no ...still work (i tested just now in Debian testing), but depend of the PDF viewer. A link to a bash script even without .sh extension with chmod +x work fine with xpdf, only ask for a "OK" to launch), while okular looks at the mimetypes and refuse launch a ejecutable external file, but evince open the script as text , while acroread launch a warning and allow only open as text. – Fran Nov 22 '14 at 8:31

This is not an answer but results of some research I've been doing. I share them in the hope somebody involved in hyperref and/or pdflatex can do something with them.

The result of simple file

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{hyperref}

\begin{document}
\href{run:file.txt}{test}
\end{document}


can be inspected by using qpdf --qpdf in.pdf out.pdf in any text editor. The annotation that represents the link is as follows:

/A <<
/F (file.txt)
/S /Launch
>>


Now, /F specifies files only so there is little hope here beyond Fran's approach. This is consistent with the error message I've been getting: viewers try to open a file with the given name, including parameters and the like. I guess viewers could try to interpret the passed filename as a shell command, but well.

However, investigating the PDF specification of launch actions (page 659ff) unearthes:

The optional Win, Mac, and Unix entries allow the action dictionary to include platform-specific parameters for launching the designated application. If no such entry is present for the given platform, the F entry is used instead. Table 8.54 shows the platform-specific launch parameters for the Windows platform. Parameters for the Mac OS and UNIX platforms are not yet defined at the time of publication.

So, to my understanding, something like

/A <<
/Win <<
/F (editor)
/P (+20 file.txt)
>>
/S /Launch
>>


should work under Windows today, and something similar might work under other OS someday.

• Good research, there are ways of including raw PDF code from pdf*TeX, \pdfannot so this code can be tried directly tex.stackexchange.com/a/12293/1871 . I am not yet proficient at using \pdfannot but I will try your code (I can only text on GNU/Linux at this point though) – alfC Oct 9 '13 at 17:29
• Also, I understand that if \pdfcompression is turned off, one can inspect the PDF without the need of commands like qpdf. – alfC Oct 9 '13 at 17:48
• I have been playing with the uncompressed PDF directly, and I start with /A<</F(file.txt)/S/Launch>>, as soon as I edit anything in the PDF file and the PDF get corrupted (I am using Emacs and vim, but I might be doing something wrong, do anybody know a sane way of editing the PDF to try these commands?) – alfC Oct 11 '13 at 23:06
• @alfC: I experienced the same; maybe some kind of checksum? Would make sense, I guess. So no luck with \pdfannot? – Raphael Oct 12 '13 at 9:48
• @alfC The PDF file stores the offsets of the objects into a table (xref). Therefore the offset for the start of the indirect objects (e.g. 7 0 obj ...endobj) should not change, otherwise the offset table needs to be fixed. – Heiko Oberdiek Nov 22 '14 at 3:20