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.

I have the following set-up in mind, and I hope someone could help me achieve this:

I cite entries from my .bib file. I use the hyperref package (manual) with the pagebackref option set to true, so when I run pdfLaTeX all my references point to the corresponding bibliography option and I can jump back to my reference.

During report writing this behaviour is not useful for me. I would rather have it such that a reference click would open the PDF file of the clicked article. So when I have a BibTex entry with label {mylittlepony}, I cite this with

Everybody loves chocolate \cite{mylittlepony}.

In the PDF file it says:

Everybody loves chocolate [1].

Now if I click the [1] a file called mylittlepony.pdf should open from a pre-defined folder.

This behaviour should only occur during report writing. When I want to generate the document for other people the original hyperref behaviour should occur, so an additional option should be set for the hyperref package. This is a hard restriction.

I want this to work with TexWorks' (Windows) integrated PDF-viewer as I like the syncTex functionality. This is not a hard restriction. If there are other ways of keeping a simple editor, syncTex and the custom behaviour described above I would go for that.

I tried using the \href function to reference to local files, but this does not work in the integrated PDF-viewer, only in Acrobat Reader X.

share|improve this question
    
Should \cite{something} always link to something.pdf? –  Caramdir Mar 31 '12 at 2:46
    
My skill level is not sufficient to evaluate this idea. Consider the redefinition of \cite after the loading of the hyperref package. This new definition would reference the bibtex database to get the local file location of the pdf file (stored in a 'new' field name.) This version of cite would use hyperref to put the hyperlink in place in the text. Now when you want the original behavior you just comment out the \input statement which loaded the new definition of \cite (stored in a file). –  R. Schumacher Mar 31 '12 at 3:16
    
@Caramdir: \cite{something} should only point to something.pdf if I am compiling the document during report writing as a draft version. If I compile the document for presentation it should just have the normal \cite behaviour –  Bart Arondson Mar 31 '12 at 5:43
    
@R.Schumacher ; I understand your idea, but I see two problems. One - I have never redefined a LaTeX command. Two - as mentioned in the question I want it to work with TeXWorks integrated pdf viewer. If I use hyperref to establish the local file link I can only use that link in Acrobat Reader X. Thank you though for thinking with me. –  Bart Arondson Mar 31 '12 at 5:49
add comment

1 Answer

up vote 1 down vote accepted

You can use the following code:

\usepackage{ifdraft}
\ifdraft{\let\obibcite=\bibcite}{}
\usepackage[final]{hyperref}
\makeatletter
\ifdraft{
  \let\bibcite=\obibcite
  \def\@citex[#1]#2{\leavevmode
  \let\@citea\@empty
  \@cite{\@for\@citeb:=#2\do
  {\@citea\def\@citea{,\penalty\@m\ }%
  \edef\@citeb{\expandafter\@firstofone\@citeb\@empty}%
  \if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
  \hyper@linkurl{% NEW
  \@ifundefined{b@\@citeb}{\hbox{\reset@font\bfseries ?}%
  \G@refundefinedtrue
  \@latex@warning
  {Citation `\@citeb' on page \thepage \space undefined}}%
  {\@cite@ofmt{\csname b@\@citeb\endcsname}}}%
  {\@citeb.pdf}% NEW
  }}{#1}}
}{}
\makeatother

It uses the ifdraft package to determine whether the document is in draft mode. If so, the original definition of the \bibcite macro is saved, then hyperref is loaded (with the final option, otherwise no links would be generated at all). Now, we restore the hyperref-free version of \bibcite to remove the default link to the bibliography which is normally produced. Instead, we patch the internal LaTeX command \@citex to generate a hyperlink to the corresponding PDF file - so \cite{mylittepony} links to mylittlepony.pdf now.

Example code:

\documentclass[draft]{article}

\usepackage{ifdraft}
\ifdraft{\let\obibcite=\bibcite}{}
\usepackage[final]{hyperref}
\makeatletter
\ifdraft{
  \let\bibcite=\obibcite
  \def\@citex[#1]#2{\leavevmode
  \let\@citea\@empty
  \@cite{\@for\@citeb:=#2\do
  {\@citea\def\@citea{,\penalty\@m\ }%
  \edef\@citeb{\expandafter\@firstofone\@citeb\@empty}%
  \if@filesw\immediate\write\@auxout{\string\citation{\@citeb}}\fi
  \hyper@linkurl{% NEW
  \@ifundefined{b@\@citeb}{\hbox{\reset@font\bfseries ?}%
  \G@refundefinedtrue
  \@latex@warning
  {Citation `\@citeb' on page \thepage \space undefined}}%
  {\@cite@ofmt{\csname b@\@citeb\endcsname}}}%
  {\@citeb.pdf}% NEW
  }}{#1}}
}{}
\makeatother

\begin{document}
Everybody loves chocolate \cite{mylittlepony}.

\begin{thebibliography}{1}
\bibitem{mylittlepony}
My bibliography entry.
\end{thebibliography}
\end{document}

(The environment thebibliography is solely for making a self-contained example, you can replace it with your own \bibliography{...} without harm.)

This code is tested with TeXworks 0.4.3 on Windows 7, where it opens mylittlepony.pdf with the default PDF viewer. Note that it may not work with other versions of TeXworks or different operating systems: Unfortunately, the internal viewer has a very limited support for hyperlinks - direct file links are apparently not working, so this solution creates an URL link instead, which works for TeXworks, may break with other viewers on the other hand.

share|improve this answer
    
Thanks a lot for your effort. However it's not entirely clear to me how I can define where the .pdf files reside. I am also used to using a .bib file for my references by calling \bibliography instead of a list of \bibitem in the main file. Does this change the code? –  Bart Arondson Apr 1 '12 at 1:05
    
Nevermind my previous comment, it works fine with \bibliography as well. Thank you very much. –  Bart Arondson Apr 1 '12 at 1:15
1  
By changing the line {\@citeb.pdf} into ` {/refs/\@citeb.pdf}` I can now put my pdf files in the root/refs directory. This makes this code perfect. –  Bart Arondson Apr 1 '12 at 1:23
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.