重置 Tikz 样式

重置 Tikz 样式

当我在中使用指定 的\CircleText宏时,会应用于。有没有办法重置样式,以便不会继承样式。下面的 MWE 产生:\nodetext width=text width=\CircleText\CircleTexttext width=

在此处输入图片描述

我认为,如果在选项开始时\CircleText,我可以以某种方式重置全部样式恢复为默认设置,这样就可以正常工作。

笔记:

代码:

\documentclass{article}
\usepackage{tikz}

%% https://tex.stackexchange.com/questions/15334/getting-numbers-to-appear-in-circles
\newcommand*{\CircledText}[2][fill=red!40]{%
    \tikz[baseline=(char.base)]{%
        \node[
            shape=circle,
            draw=black, thick, 
            fill opacity=0.3,
            text opacity=1,
            inner sep=0.8pt,
            outer sep=0pt,
            #1
        ] (char) {#2};}%
}%

\newcommand*{\DesiredText}{\CircledText{3} Some text}


\begin{document}
Outside of tikzpicture: \DesiredText

\bigskip
Inside of tikzpicture \emph{without} ``text width='' specified):

\begin{tikzpicture}
    \node at (4,0) {\DesiredText};
\end{tikzpicture}

\bigskip
Inside of tikzpicture \emph{with} ``text width='' specified):

\begin{tikzpicture}
    \node [text width=5.0cm] at (4,0) {\DesiredText};
\end{tikzpicture}
\end{document}

答案1

您可以保护想要保留的参数:

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}

\newbox\mybox

%% https://tex.stackexchange.com/questions/15334/getting-numbers-to-appear-in-circles
\newcommand*{\CircledText}[2][fill=red!40]{%
    \setbox\mybox=\hbox{#2}
    \tikz[baseline=(char.base)]{%
        \node[
            shape=circle,
            draw=black, thick, 
            fill opacity=0.3,
            text opacity=1,
            inner sep=0.8pt,
            outer sep=0pt,
            #1,text width=\wd\mybox
        ] (char) {#2};}%
}%

\newcommand*{\DesiredText}{\CircledText{3} Some text}


\begin{document}
Outside of tikzpicture: \DesiredText

\bigskip
Inside of tikzpicture \emph{without} ``text width='' specified):

\begin{tikzpicture}
    \node at (4,0) {\DesiredText};
\end{tikzpicture}

\bigskip
Inside of tikzpicture \emph{with} ``text width='' specified):

\begin{tikzpicture}
    \node [text width=5.0cm] at (4,0) {\DesiredText};
\end{tikzpicture}
\end{document}

相关内容