5

I am trying to type math exercises by using XeLaTeX, the tikz-cd package for commutative diagrams, the babel package for German, and the exsheets package for formatting and organizing the questions and answers. Here is a minimal example:

\documentclass{article}

\usepackage{xltxtra}
\usepackage{exsheets}
\usepackage[ngerman]{babel}
\usepackage{tikz-cd}
\usetikzlibrary{babel}

\begin{document}
  \begin{question}
    \[
      \begin{tikzcd}[ampersand replacement=\&]
           A \arrow[r, "f"]
        \& B
      \end{tikzcd}
    \]
  \end{question}
\end{document}

I use \usetikzlibrary{babel} because otherwise the ngerman module for babel and the " character in the tikzcd environment result in a problem similar to this one. I use ampersand replacement=\& for the tikzcd enviroment, because the question environment otherwise results in a problem similar to this one.

The given code results in the following error:

Argument of \language@active@arg" has an extra }

This is the same error one gets when not using \usetikzlibrary{babel}, and any of these three make the code work:

  • Not using the babel package.
  • Not using the question environment.
  • Removing the label "f" from the arrow.

So it seems to me that the question environment is somehow preventing \usetikzlibrary{babel} from working. I don’t know how the fix this, and the only other mention of this problem I found has a workaround specific to the Spanish language.

  • 3
    The question environment (actually a subenvironment, but it's not important) absorbs the contents as a macro to an argument; this is the reason why ampersand replacement is needed. This unfortunately also has the consequence that the trick used by the babel TikZ library doesn't work. However, you can use the alternate input \arrow{r}{f}. – egreg Jun 23 '16 at 22:45
  • @egreg: According to the tikzcd manual this notation exists for backwards compatibility, so I’m not sure if this is the best solution. But it’s a solution nonetheless, so if you post this an answer I will accept it. – Jendrik Stelzner Jun 26 '16 at 13:16
2

In my opinion, the tikz-cd package should offer a key for the arrow label, in addition to the "<label>" syntax. Unfortunately, the syntax label=f doesn't seem to work, with tikz-cd.

The issue here is that the question environment absorbs the text up to \end{question} as the argument to a macro (small lie, it's a subenvironment, actually, that does it), so the babel library can't really do its work.

You have two possibilities.

  1. Use the “older” syntax \arrow{r}{f}

  2. Surround the environment's text with \scantokens:

    \documentclass{article}
    
    \usepackage[ngerman]{babel}
    \usepackage{fontspec}
    \usepackage{exsheets}
    \usepackage{tikz-cd}
    \usetikzlibrary{babel}
    
    \begin{document}
    
    \begin{question}
    \[
    \begin{tikzcd}[ampersand replacement=\&]
    \scantokens{
      A \arrow[r,"f"]
      \& B
    }
    \end{tikzcd}
    \]
    \end{question}
    
    \end{document}
    

Don't use xltxtra, it's not really that useful nowadays.

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.