3

Is it possible to automatically shorten long URLs in the footcite (e.g. to 20 chars) and add a "..." after that? Is it also possible to trim a URL after the TLD?

Here are two examples:

Footcite now:

enter image description here

Footcite should be (more or less):

enter image description here

Here is my MWE:

\documentclass{scrartcl}

\usepackage[backend=biber, sorting=nyt, maxcitenames=2, maxbibnames=2, firstinits=true, isbn=false, doi=false, url=false, style=authoryear, dashed=false]{biblatex}

\renewcommand*{\multinamedelim}{\slash\space} % Delimiter should be a "/"
\appto{\biburlsetup}{\renewcommand*{\UrlFont}{\normalfont}} % show URL w/o color

\begin{filecontents}{bib_data.bib}
    @online{gs1_aisbl_epc_2016,
    title = {{{EPC Information Services}} ({{EPCIS}}) {{Standard Release}} 1.2},
    url = {https://www.gs1.org/sites/default/files/docs/epc/EPCIS-Standard-1.2-r-2016-09-29.pdf},
    urldate = {2018-03-09},
    date = {2016-09},
    author = {{GS1 AISBL}},
}
\end{filecontents}

\addbibresource{bib_data.bib}

\DeclareCiteCommand{\footciteonline}[\mkbibfootnote] 
    {\usebibmacro{prenote}}
    {\usebibmacro{citeindex}
    \printnames{author}
    (\printfield{year})
    \newunit\newblock
    \printfield{title}
    \newunit\newblock
    \usebibmacro{url+urldate}
    \newunit\newblock
    \setunit{\labelnamepunct}
}
  {\addsemicolon\space}
  {\usebibmacro{postnote}}


\begin{document}
Lorem Ipsum.\footciteonline{gs1_aisbl_epc_2016}

\printbibliography
\end{document}

Thanks for your help.

Let me know if you need more information.

  • I assume the link should still go out to the full URL, right? Do you also show these entries in the bibliography at the end? Should they have the full URL there? – moewe Mar 20 '18 at 9:30
  • Oh and: Welcome to TeX.SX! – moewe Mar 20 '18 at 9:31
  • Thanks for your welcome. Yes, the URL should be fully shown in the bibliography. Therefore I want to fix this with the "\footciteonline" to handle it separately. Maybe it is a solution to handle it with an extra field, but I think it is more useful if this will be done automatically. – Ma_S Mar 20 '18 at 9:50
3

The following code uses Biber's sourcemapping to extract the TLD of URLs with 50 or more characters using RegExp. It then writes the TLD to the verba field. \footciteonline is then set up to print the shortened URL from verba if available and not the long URL from url.

\documentclass{scrartcl}
\usepackage[backend=biber, style=authoryear, dashed=false, maxcitenames=2, maxbibnames=2, giveninits=true, uniquename=init, isbn=false, doi=false, url=false]{biblatex}
\usepackage{hyperref}

\renewcommand*{\multinamedelim}{\slash\space} % Delimiter should be a "/"
\appto{\biburlsetup}{\renewcommand*{\UrlFont}{\normalfont}} % show URL w/o color

\usepackage{filecontents}
\begin{filecontents}{\jobname.bib}
    @online{gs1_aisbl_epc_2016,
    title = {{{EPC Information Services}} ({{EPCIS}}) {{Standard Release}} 1.2},
    url = {https://www.gs1.org/sites/default/files/docs/epc/EPCIS-Standard-1.2-r-2016-09-29.pdf},
    urldate = {2018-03-09},
    date = {2016-09},
    author = {{GS1 AISBL}},
}
@online{Bundesamt2016,
  address     = {Wiesbaden},
  author      = {{Statistisches Bundesamt}},
  institution = {Statistisches Bundesamt},
  title       = {Bildungsstand der Bevölkerung},
  date        = {2016-11},% date ist besser als year und month
  url         = {https://www.destatis.de/GPStatistik/receive/DEHeft_heft_00057188},
}
@online{elk:bronto,
  address     = {London},
  author      = {Anne Elk},
  institution = {University of Monthy},
  title       = {On the Theory of Brontosauruses},
  date        = {1972},
  url         = {http://www.example.edu/~elk/bronto.pdf},
}
\end{filecontents}

\addbibresource{\jobname.bib}

\DeclareCiteCommand{\footciteonline}[\mkbibfootnote] 
  {\usebibmacro{prenote}}
  {\usedriver
     {\renewbibmacro*{url}{\iffieldundef{verba}{\printfield{url}}{\printfield{verba}}}}
     {\thefield{entrytype}}}
  {\addsemicolon\space}
  {\usebibmacro{postnote}}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \step[fieldsource=url, match=\regexp{.{50,}}, final]
      \step[fieldset=verba, origfieldval]
      \step[fieldsource=verba, match=\regexp{\A(ht|f)tp(s)?:\/\/([^/]+).*}, replace=\regexp{$1tp$2://$3/...}]
    }
  }
}

\DeclareFieldFormat{verba}{%
  \mkbibacro{URL}\addcolon\space
  \ifhyperref
    {\href{\thefield{url}}{\nolinkurl{#1}}}
    {\nolinkurl{#1}}}

\begin{document}
Lorem\footciteonline{gs1_aisbl_epc_2016} Ipsum\footciteonline{Bundesamt2016} dolor\footciteonline{elk:bronto} sit amet.

\printbibliography
\end{document}

enter image description here

  • Thanks for your very fast answer. After embedding this snippet into my code, it works fine. But: is it also possible that these "shortener" only affects the URLs which have e.g. more than 30 chars? – Ma_S Mar 20 '18 at 12:17
  • @Ma_S Updated for URLs with more than 50 chars, but of course you can tweak that to any number you like. – moewe Mar 20 '18 at 12:41
  • Thank you very much, moewe. I really appreciate your help! – Ma_S Mar 26 '18 at 9:33
  • Unfortunately, this solution does not work for me. I compile with XeLaTeX and use a modified BibLaTeX style (github.com/dmsul/biblatex-aer). Does anybody may elaborate on this? – Hazards Jun 24 at 12:12
  • @Hazards The solution is to some degree dependent on the style you use. I suggest you ask a new question explaining what you did and how it did not work for you. I just double-checked that the MWE in the answer still produces the desired output, so there does not seem to be an issue with the code as such. – moewe Jun 24 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.