1

I'm trying to write a simple commutative diagram like A \arrow[r, "f"] & B but I can't do it inside xsim's exercise environment (the combination of the exercise environment with the babel package, tikz package and quotation marks doesn't work).

Despite having tried everythting, I could only narrow down the problem. Could you help me with the last step of the puzzle?

PS: I leave a minimal example below of what worked and didn't work. Thank you for your help!

\usepackage{xsim}
\usepackage{tikz-cd}
\usepackage[portuguese]{babel}
\usetikzlibrary{babel}

\begin{document}

%With quotation marks, it works outside the exercise environment: $\begin{tikzcd} A \arrow[r, "f"] & B \end{tikzcd}$

%Without quotation marks, it works inside the exercise environment (with ampersand replacement): \begin{exercise} $\begin{tikzcd}[ampersand replacement=&] A \arrow[r] & B \end{tikzcd}$ \end{exercise}

%With quotation marks, it doesn't work inside the exercise environment: \begin{exercise} $\begin{tikzcd}[ampersand replacement=&] A \arrow[r, "f"] & B \end{tikzcd}$ \end{exercise}

\end{document}

1 Answers1

1

This is the same problem as in

Here are three solutions:

  1. Use the use-files class option.

    This will write additional files for every exercise environment (though there are options to tidy up your document folder) but it allows you to use both " and & as if the CD were written in the normal part of the document.

  2. Use

    \AtBeginEnvironment{exercise}{\shorthandoff{"}}
    

    to switch off the " shorthand inside the exercise environment:

    \documentclass{article}
    \usepackage{xsim}
    \usepackage{tikz-cd}
    \usepackage[portuguese]{babel}
    \usetikzlibrary{babel}
    \AtBeginEnvironment{exercise}{\shorthandoff{"}}
    \begin{document}
    \begin{exercise}
    % Need to use \babelshorthand{"-} instead of "-
    \begin{tikzcd}[ampersand replacement=\&]
    A \arrow[r, "f"] \& B
    \end{tikzcd}
    \end{exercise}
    \end{document}
    

    enter image description here

    That's not really bad unless you need to use the shorthands often inside an exercise environment.

  3. As an alternative, here's an implementation that does not need to turn off the " shorthand but uses ' instead of ".

    This is a TikZ-CD only solution and it also essentially disables the TikZ shorthand ' for swap inside the argument for \arrow but not as part of a label (as in 'f'').

    \documentclass{article}
    \usepackage{xsim}
    \usepackage{tikz-cd}
    \usepackage[portuguese]{babel}
    \usetikzlibrary{babel}
    \makeatletter
    \tikzcdset{
      every label/.prefix code=% disable ' first char again to allow ' = swap
        \pgfkeyslet{/handlers/first char syntax/\expandafter\meaning\string'}%
                   \pgfutil@undefined,
      execute before arrows={%
        \pgfkeyssetvalue{/handlers/first char syntax/\expandafter\meaning\string'}
                        {\tikzcd@forward@singlequotes}}}
    \def\tikzcd@forward@singlequotes#1{\tikzcd@forward@@singlequotes#1\pgf@stop}
    \def\tikzcd@forward@@singlequotes'#1'#2\pgf@stop{%
      \tikzcd@forward@quotes{"{#1}"#2}}
    \makeatother
    

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

    enter image description here

Qrrbrbirlbel
  • 119,821