TransWikia.com

Expanding commands to build an href from variables passed in

TeX - LaTeX Asked by bu5hman on May 24, 2021

I routinely have a large collections (thousands) of pdf documents and a bash script that compiles them into a bib file based upon the file naming system. In this process the script previously generated a static href to the document in howpublished that went into a footnote citation. The static href always opened the linked document (which may be 100’s of pages long) at page 1, yet there may be tens of references into any single document, each to a different page, so a reader has to hunt.

The previous static (working) links created by the script were of the form

howpublished={href{run:./biblio/161229.bcw.bmg.mom.0001.Bundle.11.footnotes.ocr.pdf}{[link]}} 

To eliminate the need for a reader to hunt, I am trying to pass a page number to each footnote along with the bib index so that an href can be composed to open any document at any page.

For any document, the path and filename are static and easily pulled from the bib file but when I try to compose an href these "inner" commands are not expanded and href breaks.

Is there a way to compose an href using sub-commands in a way similar to the commented out line in the MWE below?

% TeXstudio 3.1.2 (git n/a)
% Using Qt Version 5.15.2, compiled with Qt 5.15.2 R

documentclass[oneside,a4paper,12pt]{article}
usepackage{hyperref}
usepackage{biblatex}

addbibresource{bib.bib}


% usage hdetail{bibkey}{page}{commentary text}
newcommand{hdetail}[3]{
    footnote{cite[Appendix A-thesection][]{#1} 
        citeauthor{#1}'s citefield{#1}{note} of citedate{#1},  #3
% I want this ....
        citefield{#1}{howpublished}#page=#2
% question edited to change ?page to #page 
% to expand inside this .....
%       href{citefield{#1}{howpublished}#page=#2}{Link}
    }
}


begin{document}
    section{A Section}
    An item.hdetail{161206.bmg.bcw.eml.1029}{1}{Something to say about it}
end{document}

bib.bib currently looks like this

@misc{161206.bmg.bcw.eml.1029,author={{Big mak}},note="Email 1029",title={161206.bmg.bcw.eml.1029.Bundle.10.footnotes.ocr.pdf},date="2016-12-06",howpublished={./biblio/161206.bmg.bcw.eml.1029.Bundle.10.footnotes.ocr.pdf}}
@misc{161227.bmg.bcw.let.0002,author={{Big mak}},note="Letter 0002",title={161227.bmg.bcw.let.0002.Bundle.30.footnotes.ocr.pdf},date="2016-12-27",howpublished={./biblio/161227.bmg.bcw.let.0002.Bundle.30.footnotes.ocr.pdf}}
@misc{161229.bcw.bmg.mom.0001,author={{Big cojones}},note="Minutes of meeting 0001",title={161229.bcw.bmg.mom.0001.Bundle.11.footnotes.ocr.pdf},date="2016-12-29",howpublished={./biblio/161229.bcw.bmg.mom.0001.Bundle.11.footnotes.ocr.pdf}}

Corollary
While the accepted answer was 100% correct in achieving the expansion, href would not successfully parse and compile the # in the external anchor. Using url rather than href does parse the # so the stopgap solution is

newcommand*{mypage}{}
DeclareFieldFormat{howpublished:page}{tinyurl{#1#page=mypage}}

One Answer

biblatex's data printing commands are not expandable, so it is usually safer to have all the processing happen 'inside biblatex' via field formats and the like.

Here an added difficulty is that we want to combine .bib data with an argument from a macro. A quick way to combine the two is to save the argument in a macro and use that in the field format later. Luckily href is quite forgiving in what it accepts as argument so that works.

documentclass[oneside,a4paper,12pt]{article}
usepackage{biblatex}
usepackage{hyperref}

newcommand*{mypage}{}
DeclareFieldFormat{howpublished:page}{href{#1#page=mypage}{Link}}

newcommand{hdetail}[3]{%
    footnote{cite[Appendix A-thesection][]{#1} 
        citeauthor{#1}'s citefield{#1}{note} of citedate{#1},  #3
        defmypage{#2}%
        citefield{#1}[howpublished:page]{howpublished}%
    }%
}


begin{filecontents}{jobname.bib}
@misc{161206.bmg.bcw.eml.1029,
  author       = {{Big mak}},
  note         = "Email 1029",
  title        = {161206.bmg.bcw.eml.1029.Bundle.10.footnotes.ocr.pdf},
  date         = "2016-12-06",
  howpublished = {./biblio/161206.bmg.bcw.eml.1029.Bundle.10.footnotes.ocr.pdf},
}
@misc{161227.bmg.bcw.let.0002,
  author       = {{Big mak}},
  note         = "Letter 0002",
  title        = {161227.bmg.bcw.let.0002.Bundle.30.footnotes.ocr.pdf},
  date         = "2016-12-27",
  howpublished = {./biblio/161227.bmg.bcw.let.0002.Bundle.30.footnotes.ocr.pdf},
}
@misc{161229.bcw.bmg.mom.0001,
  author       = {{Big cojones}},
  note         = "Minutes of meeting 0001",
  title        = {161229.bcw.bmg.mom.0001.Bundle.11.footnotes.ocr.pdf},
  date         = "2016-12-29",
  howpublished = {./biblio/161229.bcw.bmg.mom.0001.Bundle.11.footnotes.ocr.pdf},
}
end{filecontents}
addbibresource{jobname.bib}
addbibresource{biblatex-examples.bib}

begin{document}
    section{A Section}
    An item.hdetail{161206.bmg.bcw.eml.1029}{1}{Something to say about it}
end{document}

Correct answer by moewe on May 24, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP