0

I was making a new document and I have the problem that I still don't know the style of bibliography that will be needed when I have to present it. It could be Vancouver style or APA style. I'm using biblatex because it seems me the most comfortable to use. However, if I use Vancouver style, I have citations like

According to person1 et al. (1) ...text...

Or, maybe, which is a bit more difficult

According to person1 et al. ^1 ...text...

which I could get just doing

According to \citeauthor{person1} \cite{person1} ...text...

According to \citeauthor{person1} \supercite{person1} ...text...

But if I change in biblatex style=apa with the same code above, I would get

According to person1 et al. (person1 et al. 2024) ...text...

Which is redundant and would be:

According to person1 et al. (2024) ...text...

I'd like to make a command that detects if style=vancouver or style=apa in biblatex options and executes a command if the first is true and another if the second is true

My idea was the following

\documentclass{article}
\usepackage[style=vancouver, backend=biber]{biblatex}
\usepackage{etoolbox}
\newif\ifvancouverstyle
\newif\ifapastyle

\ifcsstrequal{blx@bibstyle}{\string vancouver}{ \vancouverstyletrue }{ \vancouverstylefalse }

\ifcsstrequal{blx@bibstyle}{\string apa}{ \apastyletrue }{ \apastylefalse }

\newcommand{\detectstyle}{% \ifvancouverstyle % The action if the style is "vancouver" This is the Vancouver style. \fi \ifapastyle % The action if style is "apa" This is APA style. \fi }

\begin{document}

\title{Hello} \author{me} \maketitle

\detectstyle

\end{document}

For now I don't have idea about how configure this new command or even it's possible get it. However, when I write \detectstyle, nothing is printed.

The arguments are missing for now. I would be very grateful if someone could help me with this.

2 Answers2

2

For this particular use case you don't need a command to detect the citation style, you can just use \textcite.

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

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

\addbibresource{biblatex-examples.bib}

\begin{document} Then \textcite{sigfridsson} showed

\printbibliography \end{document}

With APA style you get

Then Sigfridsson and Ryde (1998) showed

If you change the style=apa to style=vancouver, you get

Then Sigfridsson and Ryde [1] showed


If you're not happy with the output \texcite gives you with style=vancouver and you have to run certain code (e.g. the one from Biblatex \textcite using a superscript reference number) only when the (cite)style is vancouver, you can use \ifdefstring\blx@cbxfile{vancouver}{<true>}{<false>} (within \makeatletter...\makeatother).

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

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

\makeatletter \ifdefstring\blx@cbxfile{vancouver} {\renewbibmacro*{textcite}{% \iffieldequals{namehash}{\cbx@lasthash} {\mkbibsuperscript{\supercitedelim}} {\cbx@tempa \ifnameundef{labelname} {\printfield[citetitle]{labeltitle}} {\printnames{labelname}}}% \ifnumequal{\value{citecount}}{1} {} {}% \mkbibsuperscript{\usebibmacro{cite}}% \savefield{namehash}{\cbx@lasthash}% \gdef\cbx@tempa{\addspace\multicitedelim}}%

\DeclareCiteCommand{\textcite} {\let\cbx@tempa=\empty \undef\cbx@lasthash \iffieldundef{prenote} {} {\BibliographyWarning{Ignoring prenote argument}}% \iffieldundef{postnote} {} {\BibliographyWarning{Ignoring postnote argument}}} {\usebibmacro{citeindex}% \usebibmacro{textcite}} {} {}} {} \makeatother

\addbibresource{biblatex-examples.bib}

\begin{document} Then \textcite{sigfridsson} showed

\printbibliography \end{document}

Then Sigfridsson and Ryde^1 showed

With style=apa, this document will show the APA output from above.

moewe
  • 175,683
  • Thank you for you reply. @moewe, it's very helpful to me, but I think I really need it because probably I need to use \supercite{} command instead \cite{} for Vancouver style so if you know any other way to get it, I would very much appreciate your support. I'll modify my question to include this issue. – Michael Rosales Vilca Mar 09 '24 at 17:10
  • @MichaelRosalesVilca You can use higher-level commands like \autocite, which can be set up to generate output like \supercite or \parencite depending on a simple global switch. – moewe Mar 09 '24 at 17:16
  • @MichaelRosalesVilca In your use case, you probably just want to change the definition of \textcite a little as shown in https://tex.stackexchange.com/q/114833/35864. – moewe Mar 09 '24 at 17:21
  • Really, thank you. It's working well. Just out of curiosity, do you know why my attemp to create the command \detectstyle is not working? That still worries me – Michael Rosales Vilca Mar 09 '24 at 18:30
  • @MichaelRosalesVilca Your code is not working because your assumption about what \ifcsstrequal does and thus your usage of \ifcsstrequal is wrong. Look at section "3.6.1 Macro Tests" of the documentation, "The etoolbox Package", to see what \ifcsstrequal does and which command to use instead. – Ulrich Diez Mar 10 '24 at 02:36
  • @MichaelRosalesVilca \ifcsstrequal compares to the expansion of two cs names ("macro names"), but you want to compare one command to a string, which can be done with \ifdefstring as shown here or with \ifcsstring (where you give the cs name instead). – moewe Mar 10 '24 at 06:35
0

Seems at the time of writing this answer there is no official interface for obtaining information about the style in use with biblatex. (But maybe the author of this answer overlooked that. Or things changed since this answer was written.) So, in order to get that, once more one needs to commit the crime of trying to resort to internals which might be subject to change.


Probably you can get what you need by comparing the replacement text/the expansion of the macro \blx@cbxfile via \ifcsstring:

\documentclass{article}
\usepackage[style=vancouver, backend=biber]{biblatex}
\usepackage{etoolbox}
\newif\ifvancouverstyle
\newif\ifapastyle

\ifcsstring{blx@cbxfile}{vancouver}{% \vancouverstyletrue }{% \vancouverstylefalse } \ifcsstring{blx@cbxfile}{apa}{% \apastyletrue }{% \apastylefalse }

\newcommand{\detectstyle}{% \ifvancouverstyle % The action if the style is "vancouver" This is the Vancouver style. \fi \ifapastyle % The action if style is "apa" This is APA style. \fi }

\begin{document}

\title{Hello} \author{me} \maketitle

\detectstyle

\end{document}

Alternatively expl3's \str_case:onF might be of interest:

\begin{filecontents}{\jobname.bib}
@article{person1,
  title = {Elaborate},
  author = {Person, One and Person, Two and Person, Three},
  date = {2024},
}
}
\end{filecontents}

\documentclass{article} \usepackage[% style=vancouver, % style=apa, backend=biber, maxcitenames=2, ]{biblatex}

\addbibresource{\jobname.bib}

\ExplSyntaxOn \cs_new:Npn \biblatexstylecases { \exp_args:Nc \str_case:onF {blx@cbxfile} } \ExplSyntaxOff

\biblatexstylecases{% {vancouver}{\NewDocumentCommand{\MyCommand}{m}{\citeauthor{#1}\cite{#1}}}% {apa}{\NewDocumentCommand{\MyCommand}{m}{\cite{#1}}}% }{% \message{^^JWarning: Style in use is not in cases list, thus \string\MyCommand\space not defined!^^J}% }%

\begin{document}

\title{Hello} \author{me} \maketitle

According to \MyCommand{person1} ...text...

\printbibliography[heading=bibintoc]

\end{document}


Needless to say that I am not really fond of this way of approaching the matter as the "official" and better route to go for defining citing-commands is via \DeclareCiteCommand and \renewbibmacro and the like. That's also why I did not provide code for delivering an elaborate error message in case the style in use is not in the cases list. Moewe in the comments gave good reasons for sticking to the means for defining citing-commands provided by the package biblatex itself. In my examples I did not lay focus on how to define citing-commands properly. This can be learned from the biblatex manual. Instead, in my answer the focus is on how forking things depending on the style in use could be implemented because in a comment you said you think you "really need it".

Ulrich Diez
  • 28,770
  • Wow, this is great. Thank you, @UlrichDiez I wish I could have as good a handle on Latex as you do. – Michael Rosales Vilca Mar 10 '24 at 07:05
  • 1
    I'd say that \blx@opt@eldt@style is slightly more "internal" than the \blx@cbxfile used in my answer. Granted, both are \blx@... macros, which are internal, but \blx@opt@eldt@style heavily depends on the implementation of option parsing, whereas \blx@cbxfile does not so much. Plus \blx@cbxfile will always be defined, but \blx@opt@eldt@style may end empty if a user sets bibstyle and citestyle separately (or not at all...). – moewe Mar 10 '24 at 08:14
  • 1
    I also didn't mention it in the comments or in my answer, because I could work around the issue by using \textcite, but generally it is absolutely not recommended to define new command that calls several \...cite macros at once. For one a simple definition like \NewDocumentCommand{\MyCommand}{m}{\citeauthor{#1}\cite{#1}} fails to deal properly with pre and post notes (which could obviously be changed), but it can also confuse citation tracking and does not deal well with citations of multiple works. Complex citation commands should always be defined with \DeclareCiteCommand. – moewe Mar 10 '24 at 08:16
  • @moewe I edited my answer and changed to \blx@cbxfile. I also added a remark at the end. I didn't think this is necessary as, iirc, the manual of the package biblatex explains why the things provided there are useful. – Ulrich Diez Mar 10 '24 at 13:53
  • @MichaelRosalesVilca My handle on LaTeX highly depends on my daily condition. ;-) When implementing your things, also obey Moewe's remarks. – Ulrich Diez Mar 10 '24 at 14:01