Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply a command to pure text of journal field #974

Closed
nickkolok opened this issue Mar 16, 2020 · 2 comments
Closed

Apply a command to pure text of journal field #974

nickkolok opened this issue Mar 16, 2020 · 2 comments
Labels

Comments

@nickkolok
Copy link

I have some articles like this:

@article{erdos1945integral,
  title={Integral distances},
  author={Erd{\H{o}}s, Paul},
  doi={10.1090/S0002-9904-1945-08490-0},
  journal={Bulletin of the American Mathematical Society},
  volume={51},
  number={12},
  pages={996},
  year={1945}
}

And (for example) a simple command defined like this:

\newcommand\testchain[1]{%
\StrLeft{#1}{3}[\firstchar]%
\firstchar%
}

Indeed, it is much more complex, but still invokes xstring and operates on strings.

I need to apply this command to the pure Bulletin of the American Mathematical Society string.

Attempt 1.

\DeclareFieldFormat[article]{journal}{\testchain{#1}}

No effect (AFAIK journal is just an alias for journaltitle, isn't it?)

Attempt 2.

\DeclareFieldFormat[article]{journaltitle}{\testchain{#1}}

Error:

Runaway argument?
t\blx@endunit \setunit *{\addspace }\iffieldundef {series} {} {\newunit \ETC.
! Paragraph ended before \\blx@imc@printfield was complete.
<to be read again> 
                   \par 
l.41 
     
? 
! Emergency stop.
<to be read again> 
                   \par 
l.41 
     
End of file on the terminal!

OK... let's see what's the problem:

\DeclareFieldFormat[article]{journaltitle}{\detokenize{#1}}

We obtain:

\printfield [titlecase]{journaltitle}\setunit{\subtitlepunct }\printfield [titlecase]{journalsubtitle}

Attempt 3

How about a little game with expansion?
Unfortunately, we get the same result (as in Attempt 2) for

\DeclareFieldFormat[article]{journaltitle}{\expandafter\detokenize{#1}}

and no effect (as in Attempt 1) for

\DeclareFieldFormat[article]{journaltitle}{\expandafter\detokenize{#1}}

Attempt 4

\DeclareFieldFormat[article,journal]{titlecase}{\testchain{\detokenize{#1}}}

(it throws a \GenericError without \detokenize).
Both journal title and article title are truncated to 3 letters:
Paul Erdős. ‘Int’. In: Bul 51.12 (1945), p. 996. doi: 10.1090/S0002-9904- 1945-08490-0.

How do I achieve the desired result and what BibLaTeX versions allow me to do that?

P.S. I can post this question to TeX.SX if you prefer.

@moewew
Copy link
Collaborator

moewew commented Mar 16, 2020

The formatting for titles is a bit special, mainly because they often consist of two parts ...title and ..subtitle and because we may have to apply sentence casing to them, which is quite a sensitive macro (just like your \testchain).

For journaltitle we have

\newbibmacro*{journal}{%
\ifboolexpr{
test {\iffieldundef{journaltitle}}
and
test {\iffieldundef{journalsubtitle}}
}
{}
{\printtext[journaltitle]{%
\printfield[titlecase]{journaltitle}%
\setunit{\subtitlepunct}%
\printfield[titlecase]{journalsubtitle}}}}

Attempt 1 doesn't work because there is no field (and no field format) called journal. Internally there are only the fields journaltitle and journalsubtitle. (On the .bib file level journal is an alias that gets mapped to journaltitle, but that mapping is resolved by the backend. biblatex styles only know and get to see journaltitle.)

Attempt 2 uses an existing field format, but breaks because \testchain is a macro that can not accept arbitrary input. In particular your \DeclareFieldFormat[article]{journaltitle}{\detokenize{#1}} makes visible what the macro actually gets passed as an argument when you do \DeclareFieldFormat[article]{journaltitle}{\testchain{#1}}.

Unfortunately \printfield is not expandable, so no number of \expandafters or \edefs can help \testchain deal with

\printfield [titlecase]{journaltitle}\setunit{\subtitlepunct }\printfield [titlecase]{journalsubtitle}

This means Attempt 3 won't work (and also that no further variation on Attempt 3 will work).

A variant on Attempt 4 is the way to go here. You need to operate on the raw journaltitle field, so you can't redefine the journaltitle field format, which applies to more than just journaltitle. titlecase applies to all title-like fields, though, so you need to redefine the bibmacro journal shown above to apply a unique format. For example

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}
\usepackage{csquotes}

\usepackage[style=authoryear, backend=biber]{biblatex}

\usepackage{xstring}
\newcommand\testchain[1]{%
  \StrLeft{#1}{3}[\firstchar]%
  \firstchar%
}

\DeclareFieldFormat{journaltitle:nickkolok}{\testchain{#1}}

\renewbibmacro*{journal}{%
  \ifboolexpr{
    test {\iffieldundef{journaltitle}}
    and
    test {\iffieldundef{journalsubtitle}}
  }
    {}
    {\printtext[journaltitle]{%
       \printfield[journaltitle:nickkolok]{journaltitle}%
       \setunit{\subtitlepunct}%
       \printfield[journalsubtitle:nickkolok]{journalsubtitle}}}}

\addbibresource{biblatex-examples.bib}

\begin{document}
\cite{sigfridsson}
\printbibliography
\end{document}

For future issues that are not feature requests (like #968) or bug reports, I would indeed prefer if you would ask on TeX.SX or your favourite LaTeX help site.

I would also really appreciate if you could include a small yet compilable example document instead of just code snippets that have to be pieced together to create a working document.

@nickkolok
Copy link
Author

Thank you very much, the solution works perfectly!

@moewew moewew closed this as completed Mar 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants