1

We have worked on a research proposal and planned to submit also a flash drive with a pdf of the proposal and all our referenced papers as local copies. We asked our self, if it is possible to link the bibliography entries in the proposal to the local pdf files using hyperlinks. Ideally, the linked pdfs should open in a new window to keep the main document (proposal) open. Furthermore, we needed every free space we could get. Thus, long bibliography entries, caused by DOI and URLs, need to be reduced.

Question: How to achieve this with a timesaving latex macrocode?

1 Answer 1

2

We created a macro that solves all these problems and creates a structure like "Links: web, local", where web is a hyperlink to doi or url (depending on available fields of the related bibtex entry) and local links to the local pdf by using a relative path.

At the moment, our code has a limitation: the macro searches for bibkey isntead of a file name maybe presented in hte field file

description of the code: Each bibliography entry is checked and eventually modified. If the field URL is defined, use it to create hyperlink for "web". If no URL exists, use DOI to generate link. If no URL and no DOI exist, don't add the text "web" to the bib entry. Check for an existing pdf file in a specified folder. If it exists, add text "local" to bib entry and hyperlink this text to the pdf file using relative paths.

current limitation: the code checks only for pdfs, which are named like the bibtex key in the related bibtex entry. In future, there could also be a test for the field FILE. If it exists, try to extract the filename form the string.

features: We added the possiblity to use sub-directories for a better structure of the pdfs if the proposal is huge and several bibliographies (several /printbibliography calls) are used. You can use the command \RelPathSet{folder} to switch the folder for the pdf search and path creation. IMPORTANT: This can't be used to change the pdf sub-folder for each \cite. it can only be used to change the folder for a /printbibliography command. Web and pdf link are only visible if DOI field , URL field or pdf file is available.

code:

    \documentclass{article}
    \usepackage[doi=false,isbn=false,url=false, backend=bibtex]{biblatex}
    \usepackage[hidelinks]{hyperref}
    \usepackage{filecontents} % create dummy bib-files

    \begin{filecontents}{general.bib}
        @article{adams,
            author  = {Peter Adams}, 
            title   = {The title of the work},
            journal = {The name of the journal},
            year    = 1993,
            number  = 2,
            pages   = {201-213},
            month   = 7,
            note    = {An optional note}, 
            volume  = 4
        }
        @article{beta,
            author  = {Luke Beta}, 
            title   = {The title of the work},
            journal = {The name of the journal},
            year    = 1993,
            number  = 2,
            pages   = {201-213},
            month   = 7,
            note    = {An optional note}, 
            volume  = 4,
            doi     = 02312312313
        }

        @article{chameli,
            author  = {August Chameli}, 
            title   = {The title of the work},
            journal = {The name of the journal},
            year    = 1993,
            number  = 2,
            pages   = {201-213},
            month   = 7,
            note    = {An optional note}, 
            volume  = 4,
            url     = "www.google.com"
        }

        @article{delta,
            author  = {Mike Delta}, 
            title   = {The title of the work},
            journal = {The name of the journal},
            year    = 1993,
            number  = 2,
            pages   = {201-213},
            month   = 7,
            note    = {An optional note}, 
            volume  = 4,
            doi     = 02312312313,
            url     = "www.google.com"
        }
        @article{elbing,
            author  = {Frank Elbing}, 
            title   = {The title of the work},
            journal = {The name of the journal},
            year    = 1993,
            number  = 2,
            pages   = {201-213},
            month   = 7,
            note    = {An optional note}, 
            volume  = 4
        }       
    \end{filecontents}

    \addbibresource{general.bib}

    % macro for space-saving hyperlinks to website and local file   %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    \hypersetup{pdfnewwindow=true} % open linked pdfs in a new window, instead of replaceing the main document
    \newcommand{\RelPath}{}
    %\newcommand{\RelPathSet}[1]{\renewcommand{\RelPath}{../path_to_main_pdf_folder/#1/}} % example: pdf folder is "outside"- no sub-directory of latex work folder
    \newcommand{\RelPathSet}[1]{\renewcommand{\RelPath}{path_to_main_pdf_folder/#1/}} % example: pdf folder is a sub-directory of latex work folder
    \RelPathSet{sub_folder} % initial path setup

    \AtEveryBibitem{

        \iffieldundef{url}
        {%field url is not defined, test if field doi is defined
            \iffieldundef{doi}
            {%field doi is also not defined, test if pdf file exists
                \IfFileExists{\RelPath\thefield{entrykey}.pdf}
                {%only file exists, create hyperlink with relative path to pdf file 
                    \csappto{blx@bbx@\thefield{entrytype}}{\addspace{{Link: }\href{run:\RelPath\thefield{entrykey}.pdf}{local}}}
                }
            }
            {%field doi is defined, test for field file
                \IfFileExists{\RelPath\thefield{entrykey}.pdf}
                {%field doi definde and file exists, use the relative file path and generate url from doi
                    \csappto{blx@bbx@\thefield{entrytype}}{\addspace{{Links: }\href{run:\RelPath\thefield{entrykey}.pdf}{local}, \href{http://dx.doi.org/\thefield{doi}}{web}}}
                }
                {%file not available, generate url from doi and link only this at the end of the current entry
                    \csappto{blx@bbx@\thefield{entrytype}}{\addspace{{Link: }\href{http://dx.doi.org/\thefield{doi}}{web}}}
                }
            }
        }
        {%url is definded, test if pdf file exists
            \IfFileExists{\RelPath\thefield{entrykey}.pdf}  
            {%field url defined and file exists
                \csappto{blx@bbx@\thefield{entrytype}}{\addspace{{Links: }\href{run:\RelPath\thefield{entrykey}.pdf}{local}, \href{\thefield{url}}{web}}}
            }
            {%field url defined, file not available
                \csappto{blx@bbx@\thefield{entrytype}}{\addspace{{Link: }\href{\thefield{url}}{web}}}
            }
        }
    }

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

    \begin{document}


        Lorem ipsum dolor sit amet, consectetuer adipiscing elit \cite{adams}. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel \cite{beta}, aliquet nec, vulputate eget, arcu. In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibu \cite{chameli}. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus \cite{delta}. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a, tellus. Phasellus viverra nulla ut metus varius laoreet \cite{elbing}.

\RelPathSet{sub_folder} % set name of sub folder, if the pdfs are stored in different folders       
\printbibliography
    \end{document}

output: enter image description here

edit:

Tutorial for semi-automatic bibtex entry generation, pdf file download and required renaming to fit requirements of the posted latex code using JabRef:

  • Download and start JabRef
  • Go to Options->Preferences->File and set the folder for the local pdf files as ‘Main file directory’
  • Go to Options->Preferences->Import and set ‘Filename format pattern’ as required by the latex code, using the latex code above ‘\bibtexkey’ (without ‘’) needs to be entered here (remove existing content, if not empty)
  • open the existing bibtex file (.bib) or create a new one, the following description is based on an existing bibtex file
  • double click on an existing entry, choose the tab ‘General’ and do the following:
  • click on ‘Lookup DOI’ (if no DOI can be found, google the related DOI and insert it manually; if no DOI exists, insert URL manually; if also no URL exists, use ‘+’ button and add PDF file manually)
  • click on ‘Get BibTeX data from DOI’ and decide which information should be replaced in the existing entry – do not replace the existing bibtex key
  • click on ‘Get fulltext’ and save file to displayed folder or create sub-folder, if no PDF can be found, try to download it with ‘Download from URL’

repeat this procedure for all needed entries and save changes to bibtex file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .