tikz inside expl3

tikz inside expl3

我无法弄清楚在这个基本示例中我做错了什么。我想在我的 expl3 代码中有一个函数使用 tikz 绘制一个简单的图片,但我收到错误Package pgf Error: No shape named 0:0 is known.

平均能量损失

\documentclass{extbook}

\usepackage{tikz}
    \usetikzlibrary{arrows,automata,positioning,calc}

% my command to draw an orange rectangle with tikz
\newcommand{\drawmyshape}{
    \tikz[font=\normalsize]{
        \draw[color=orange!50, fill=orange] (0:0) -- ++(\linewidth,0) -- ++(0,-2em) -- (0,-2em) -- (0,0);
    }%%%%%
}%%%%%

\usepackage{xparse}%allows the NewDocumentCommand

\ExplSyntaxOn

%Why does the tikz code inside the expl3syntax break it? I need to be able to call this here.
\NewDocumentCommand{\drawmyshapeagain}{}{
    \tikz[font=\normalsize]{
        \draw[color=orange!50, fill=orange] (0:0) -- ++(\linewidth,0) -- ++(0,-2em) -- (0,-2em) -- (0,0);
    }%%%%%
}%%%%%

\ExplSyntaxOff

\begin{document}

\drawmyshape

\drawmyshapeagain

\end{document}

答案1

在 expl3 中,冒号是一个字母,这会破坏代码中的 (0:0)。您在示例中没有使用 expl3 命令,因此最简单的方法是删除该\ExplSyntaxOn命令,但如果您需要它来做其他事情,您可以使用它(在这种情况下,这并不总是有效)\c_colon_str

\documentclass{extbook}

\usepackage{tikz}
    \usetikzlibrary{arrows,automata,positioning,calc}

% my command to draw an orange rectangle with tikz
\newcommand{\drawmyshape}{
    \tikz[font=\normalsize]{
        \draw[color=orange!50, fill=orange] (0:0) -- ++(\linewidth,0) -- ++(0,-2em) -- (0,-2em) -- (0,0);
    }%%%%%
}%%%%%

\usepackage{xparse}%allows the NewDocumentCommand

\ExplSyntaxOn

%Why does the tikz code inside the expl3syntax break it? I need to be able to call this here.
\NewDocumentCommand{\drawmyshapeagain}{}{
    \tikz[font=\normalsize]{
        \draw[color=orange!50, fill=orange] (0\c_colon_str0) -- ++(\linewidth,0) -- ++(0,-2em) -- (0,-2em) -- (0,0);
    }%%%%%
}%%%%%

\ExplSyntaxOff

\begin{document}

\drawmyshape

\drawmyshapeagain

\end{document}

相关内容