# BibLaTeX footcite shorten long URL

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:

Footcite should be (more or less):

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}

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

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

\printbibliography
\end{document}


• 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? Mar 20, 2018 at 9:30
• Oh and: Welcome to TeX.SX! Mar 20, 2018 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, 2018 at 9:50

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,
author      = {{Statistisches Bundesamt}},
institution = {Statistisches Bundesamt},
title       = {Bildungsstand der Bevölkerung},
date        = {2016-11},% date ist besser als year und month
}
@online{elk:bronto,
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}

\DeclareCiteCommand{\footciteonline}[\mkbibfootnote]
{\usebibmacro{prenote}}
{\usedriver
{\renewbibmacro*{url}{\iffieldundef{verba}{\printfield{url}}{\printfield{verba}}}}
{\thefield{entrytype}}}
{\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}{%
\ifhyperref

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

\printbibliography
\end{document}


• 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, 2018 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. Mar 20, 2018 at 12:41
• Thank you very much, moewe. I really appreciate your help!
– Ma_S
Mar 26, 2018 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? Jun 24, 2019 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. Jun 24, 2019 at 20:10