6

When I use the \CircleText macro within a \node in which the text width= is specified, the text width= gets applied to \CircleText. Is there a way to reset the style so that \CircleText does not inhert the text width= style. The MWE below produces:

enter image description here

I think, if at the start of the options to \CircleText, I could somehow reset all style to their default setting, this would work fine.

Notes:

Code:

\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}
  • I am wondering if this is related. – user121799 Mar 21 '18 at 3:56
  • @marmot: Seems so, I have correcte the title and will have a look at that link in a little while. – Peter Grill Mar 21 '18 at 3:59
  • Am I missing something here? Isn't this just a case of nesting tikzpictures, which isn't supported and would be liable to break at any time even if it happened to work in some particular case? I assume I'm missing something, because obviously you know this as well as I do. – cfr Mar 21 '18 at 4:04
  • @cfr: Yah it is nesting tikzpictures, but I did not know that until I created this MWE. But don't know how else to define a macro to work both within and outside of tikz. – Peter Grill Mar 21 '18 at 4:06
  • @PeterGrill You need to detect it. I think tikzmark would be useful to look at as, if I'm remembering correctly, it does something like this. – cfr Mar 21 '18 at 4:08
2

You can protect the paramater you want to preserve :

enter image description here

\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}
|improve this answer|||||
  • 1
    You could use text width={}, see tex.stackexchange.com/a/184894/43317. But the outer node could get other options like minimum width or dashed too. – esdd Mar 21 '18 at 8:30
  • Yep that will work and is the solution I am currently using. See Note 1 in the question. – Peter Grill Mar 21 '18 at 16:31

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.