1

I am trying to create a custom postnote in a citation for a project where I need to subtly highlight some citations. I would like to draw two semicircles, filled with different colors, and display this as a postnote next to a citation. Below is a MWE of what I am trying:

\documentclass[a4paper,BCOR=0mm,DIV=18]{scrartcl}

\usepackage{xcolor} \definecolor{mygreen}{rgb}{0.106,0.62,0.467} \definecolor{myorange}{rgb}{0.851,0.373,0.008}

\usepackage[backend=biber]{biblatex} \addbibresource{temp.bib}

\usepackage{tikz} \usetikzlibrary{positioning}

\newcommand{\mix}{{ \begin{tikzpicture} \begin{scope} \clip (0,-0.5ex) rectangle (0.5ex,0.5ex); \fill[myorange] (0,0) circle(0.5ex); \draw[myorange] (0,-0.5ex) -- (0,0.5ex); \end{scope} \begin{scope} \clip (0,0.5ex) rectangle (-0.5ex,-0.5ex); \fill[mygreen] (0,0) circle(0.5ex); \end{scope} \end{tikzpicture}}}

\begin{document}

Why does this citation \cite[\mix]{ref} not work? % comment to get it to run But this \mix works?

\end{document}

and the bibliography file:

@article{ref,
    title = {blah blah},
    journaltitle = {Journal},
}

When I put \mix outside the \cite command everything works fine, but once I put use it as a postnote I get undefined control sequence errors all over the show... Does anybody know how I can fix this?

redfuzz
  • 13
  • Welcome to TeX.SX! Using a savebox could be a quick fix: (\newsavebox{\mix} \savebox{\mix}{<tikzpicture>}, \cite[\usebox{\mix}]{ref}. – Jasper Habicht Oct 04 '22 at 22:15

2 Answers2

1

Not a real answer to your question why placing TikZ code inside the \cite macro is not working, but rather a quick fix: Use a savebox. I also simplified the code for the icon a bit.

\documentclass[a4paper,BCOR=0mm,DIV=18]{scrartcl}

\begin{filecontents}{temp.bib} @article{ref, title = {blah blah}, journaltitle = {Journal}, } \end{filecontents}

\usepackage{xcolor} \definecolor{mygreen}{rgb}{0.106,0.62,0.467} \definecolor{myorange}{rgb}{0.851,0.373,0.008}

\usepackage[backend=biber]{biblatex} \addbibresource{temp.bib}

\usepackage{tikz} \usetikzlibrary{positioning}

\newsavebox{\mix} \savebox{\mix}{% \begin{tikzpicture} \fill[myorange] (0,-0.5ex) arc[start angle=-90, end angle=90, radius=0.5ex] -- cycle; \fill[mygreen] (0,0.5ex) arc[start angle=90, end angle=270, radius=0.5ex] -- cycle; \end{tikzpicture}% }

\begin{document}

Why does this citation \cite[\usebox{\mix}]{ref} not work? But this \usebox{\mix} works?

\end{document}

enter image description here

1

The \mix macro needs to be protected, so you'd need to write

\cite[\protect\mix]{ref}

but that can get tedious to do. LaTeX also provides a way to define a macro to be protected (“robust”) in the first place:

\DeclareRobustCommand*{\mix}{% ← % important to hide space
\begin{tikzpicture}[radius=.5ex,delta angle=180]
  \fill[myorange](up:.5ex)arc[start angle=90];
  \fill[mygreen](down:.5ex)arc[start angle=270];
\end{tikzpicture}}

That said, if you use that picture often, it might be better to save it in a box so that it doesn't need to be calculated over and over again.

You could also create it once in a standalone document and save it as a tiny PDF that you then \includegraphics in your document.

Code

\begin{filecontents*}{\jobname.bib}
@article{ref,
  title = {blah blah},
  journaltitle = {Journal},
}
\end{filecontents*}
\documentclass[a4paper,BCOR=0mm,DIV=18]{scrartcl}
\usepackage{tikz}
\definecolor{mygreen}{rgb}{0.106,0.62,0.467}
\definecolor{myorange}{rgb}{0.851,0.373,0.008}
\usepackage[backend=biber]{biblatex}
\addbibresource{\jobname.bib}

\DeclareRobustCommand*{\mix}{% ← % important to hide space \begin{tikzpicture}[radius=.5ex,delta angle=180] \fillmyorangearc[start angle=90]; \fillmygreenarc[start angle=270]; \end{tikzpicture}}

\begin{document} Why does this citation \cite[\mix]{ref} not work? But this \mix\ works? \end{document}

Qrrbrbirlbel
  • 119,821