2

I use the code as in the following MWE:

\documentclass{article}

\usepackage[english]{babel}

\useshorthands*{"}
\defineshorthand{"-}{\texorpdfstring{\babelhyphen{repeat}}{\discretionary{-}{-}{-}}}

\usepackage{biblatex}
\usepackage{csquotes}

\usepackage{xstring}
\verbtocs{\lecture}|Lecture, topic: \enquote {|

\makeatletter
\newbibmacro{unpublished+titles}[1]{%
    \iffieldundef{title}%
    {#1}%
    {%
        % https://tex.stackexchange.com/a/155276:
        \edef\mytemporary{\thefield{title}}%
        \edef\mytitle{\expandafter\strip@prefix\meaning\mytemporary}%
        \IfBeginWith*{\mytitle}{\clt}%
            {#1}%
            {\mkbibquote{#1\isdot}}%
    }%
}
\makeatother
\DeclareFieldFormat[unpublished]{citetitle}{\usebibmacro{unpublished+titles}{#1}}
\DeclareFieldFormat[unpublished]{title}{\usebibmacro{unpublished+titles}{#1}}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
    @Unpublished{A1,
        author       = {Author},
        title        = {Some other work}
    }
    @Unpublished{A2,
        author       = {Author},
        title        = {Lecture, topic: \enquote{test}}
    }
    @Unpublished{A3,
        author       = {Author},
        title        = {Test"-test}
    }
\end{filecontents*}
\addbibresource{\jobname.bib}

\begin{document}
    \noindent Citations: \cite{A1}~\cite{A2}%~\cite{A3}

    \printbibliography
\end{document}

In this case it detects bibliography entries of type unpublished and if their titles start with “Lecture, topic: \enquote{”, it removes the first part of title from the bibliographical quotation. An illustrative result looks is:

Citations

Nevertheless, this works only for entries that do not include "- shorthand in their titles. If they do, the following problem occurs:

Errors

Uncommenting %~\cite{A3} in MWE causes such error.

Is it possible to expand this feature to cover also titles that contains "- shorthand? Either by correcting the code above or in other way that would produce the desired result.

2

The code expands the involved macros quite heavily, but \texorpdfstring{\babelhyphen{repeat}}{\discretionary{-}{-}{-}} is not expandable, so it breaks.

A functioning workaround that I could find is to store this definition in a robust macro and use that in the definition of "-. In the code below the expansion of the title is also kept to a minimum (by using \savefield*{title} instead of the \edef construction).

\documentclass{article}
\usepackage[english]{babel}
\usepackage{csquotes}
\usepackage{biblatex}
\usepackage{xstring}
\usepackage{hyperref}

\newrobustcmd*{\robustbutbreakablehyphen}{%
  \texorpdfstring{\babelhyphen{repeat}}{\discretionary{-}{-}{-}}}

\useshorthands*{"}
\defineshorthand{"-}{\robustbutbreakablehyphen}

\makeatletter
\verbtocs{\lecture}|Lecture, topic: \enquote {|

\newbibmacro{unpublished+titles}[1]{%
  \iffieldundef{title}%
    {#1}
    {%
       % https://tex.stackexchange.com/a/155276:
       \savefield*{title}{\mytitle}%
       \IfBeginWith*{\mytitle}{\lecture}
         {#1}
         {\mkbibquote{#1\isdot}}%
    }%
}
\makeatother

\DeclareFieldFormat[unpublished]{citetitle}{\usebibmacro{unpublished+titles}{#1}}
\DeclareFieldFormat[unpublished]{title}{\usebibmacro{unpublished+titles}{#1}}

\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@unpublished{A1,
  author       = {Author},
  title        = {Some other work},
}
@unpublished{A2,
  author       = {Author},
  title        = {Lecture, topic: \enquote{test}},
    }
@unpublished{A3,
  author       = {Author},
  title        = {Test"-test},
}
\end{filecontents*}
\addbibresource{\jobname.bib}

\begin{document}
  Citations: \cite{A1,A2,A3}

  \printbibliography
\end{document}

Author. Lecture, topic: “test”.//Author. “Some other work”.//Author. “Test-test”.


Like PLK in the linked question I would suggest a solution using Biber sourcemaps and field annotations instead of direct xstring manipulations. Biber can scan fields with Perl RegEx (and does not expand LaTeX macros, so has no issues with expandability and the linke) and thus has powerful tools for situations like this.

\documentclass{article}
\usepackage[english]{babel}
\usepackage{csquotes}
\usepackage{biblatex}
\usepackage{xstring}
\usepackage{hyperref}

\useshorthands*{"}
\defineshorthand{"-}{\texorpdfstring{\babelhyphen{repeat}}{\discretionary{-}{-}{-}}}

\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
       \step[fieldsource=title, match=\regexp{Lecture,\s+topic:\s\\enquote\s*\{}, final]
       \step[fieldset=title+an, fieldvalue={=noquotes}]
    }
  }
}

\newbibmacro{unpublished+titles}[1]{%
  \iffieldannotation[title]{noquotes}
    {#1}
    {\mkbibquote{#1\isdot}}%
}

\DeclareFieldFormat[unpublished]{citetitle}{\usebibmacro{unpublished+titles}{#1}}
\DeclareFieldFormat[unpublished]{title}{\usebibmacro{unpublished+titles}{#1}}


\usepackage{filecontents}
\begin{filecontents*}{\jobname.bib}
@unpublished{A1,
  author       = {Author},
  title        = {Some other work},
}
@unpublished{A2,
  author       = {Author},
  title        = {Lecture, topic: \enquote{test}},
    }
@unpublished{A3,
  author       = {Author},
  title        = {Test"-test},
}
\end{filecontents*}
\addbibresource{\jobname.bib}

\begin{document}
  Citations: \cite{A1,A2,A3}

  \printbibliography
\end{document}

The output is the same.

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.