pgfplots 的边注:与 usetikzlibrary{babel} 冲突

pgfplots 的边注:与 usetikzlibrary{babel} 冲突

我必须使用 tikzlibrary babel 来避免与法语 babel 包发生冲突。尽管如此,每次我尝试在边注内绘制图表时都会遇到问题(重要的:我的图表是用 pgfplots 创建的,并且我使用了 marginnote 包)。

\documentclass[dvipsnames,11pt]{article}

\usepackage{amsfonts}
\usepackage[a4paper,vmargin=2cm,left=1cm,textwidth=15cm,marginparwidth=4cm]{geometry}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{marginnote}
\usepackage[french]{babel}

\pgfplotsset{compat=1.16}
\usetikzlibrary{babel}


\begin{document}

    % The code below works (It doesn't use pgfplots).
    \lipsum[3]
    \marginnote{\begin{tikzpicture}\draw (0,0) -- (1,1); \end{tikzpicture}}
    \lipsum[4]

    % The code below doesn't work (conflict with \usetikzlibrary{babel})
    \lipsum[3]
    \marginnote{
        \begin{tikzpicture}
            \begin{axis}[width=3cm, axis lines=none]
            \addplot [
                domain=0:1, 
                samples=10, 
            ]
            {x};
            \end{axis}
        \end{tikzpicture}
        }
    \lipsum[4]

\end{document}

我收到的错误是:

Argument of \pgfplots@addplotimpl@expression@curly has an extra }. \lipsum

所以我想知道在这种情况下是否有一种安全的方法来使用 tikzlibrary babel。

如果我不使用边注,我可以\usetikzlibrary{babel}使用如下方法删除有问题的库导入行\shorthandoff这个帖子。另请参阅下面的代码:

\documentclass[dvipsnames,11pt]{article}

\usepackage{amsfonts}
\usepackage[a4paper,vmargin=2cm,left=1cm,textwidth=15cm,marginparwidth=4cm]{geometry}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{marginnote}
\usepackage[french]{babel}

\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
%\usetikzlibrary{babel}


\begin{document}

    % The code below doesn't work without \usetikzlibrary{babel} or \shorthandoff{:}
    \begin{tikzpicture}
    \shorthandoff{:}  % line added
    \begin{axis}[
        axis lines = none,
    ]
    \addplot [name path=A,domain=-5:1,samples=100] {1/pi*1/(x^2+1)};
    \addplot [name path=B,domain=-5:1] {0};
    \addplot [color=blue!30!white] fill between [
    of=A and B, soft clip = {domain = -5:1}  % clash with the babel french package because of this line.
    ];
    \addplot [
        domain=-5:5, 
        samples=100, 
        color=black,
    ]
    {1/pi*1/(x^2+1)};
    \end{axis}
    \end{tikzpicture}
\end{document}

如果我将相同的代码包装在 marginnote 命令中,则在所有情况下都会出现致命错误:

\documentclass[dvipsnames,11pt]{article}

\usepackage{amsfonts}
\usepackage[a4paper,vmargin=2cm,left=1cm,textwidth=15cm,marginparwidth=4cm]{geometry}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{marginnote}
\usepackage[french]{babel}

\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
%\usetikzlibrary{babel}


\begin{document}
% I don't know how to make this code work with or without using \usetikzlibrary{babel}...
    \marginnote{
    \begin{tikzpicture}
    %\shorthandoff{:} If I uncomment this line, I get the error: Argument of \pgfkeys@code has an extra }. \end
    \begin{axis}[
        axis lines = none,
    ]
    \addplot [name path=A,domain=-5:1,samples=100] {1/pi*1/(x^2+1)};
    \addplot [name path=B,domain=-5:1] {0};
    \addplot [color=blue!30!white] fill between [
    of=A and B, soft clip = {domain = -5:1}  
    ];
    \addplot [
        domain=-5:5, 
        samples=100, 
        color=black,
    ]
    {1/pi*1/(x^2+1)};
    \end{axis}
    \end{tikzpicture}}
\end{document}

如何解决上述问题?

答案1

我用花括号将边注括起来,并在边注之前使用了命令\shorthandoff{;:!?}(对我来说就足够了):它解决了这个问题。\shorthandoff{:}

@薛定谔的猫

@dgoodmaniii

感谢您的回答

\documentclass[dvipsnames,11pt]{article}

\usepackage{amsfonts}
\usepackage[a4paper,vmargin=2cm,left=1cm,textwidth=15cm,marginparwidth=4cm]{geometry}
\usepackage{amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{lipsum}
\usepackage{marginnote}
\usepackage[french]{babel}

\pgfplotsset{compat=1.16}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{babel}


\begin{document}

    \lipsum[3]
    {\shorthandoff{;:!?}% or \shorthandoff{:} in this case  
    \marginnote{%
    \begin{tikzpicture}
                \begin{axis}[
                    axis lines = none,
                ]
                \addplot [name path=A,domain=-5:1,samples=100] {1/pi*1/(x^2+1)};
                \addplot [name path=B,domain=-5:1] {0};
                \addplot [color=blue!30!white] fill between [
                of=A and B, soft clip = {domain = -5:1}
                ];
                \addplot [
                    domain=-5:5, 
                    samples=100, 
                    color=black,
                ]
                {1/pi*1/(x^2+1)};
                \end{axis}
    \end{tikzpicture}
    }}
    \lipsum[4]

\end{document}

相关内容