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}

Peter Grill
  • 223,288
  • I am wondering if this is related. –  Mar 21 '18 at 03: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 03: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 04: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 04: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 04:08
  • I had a similar question, I think. At least I also wanted to create a macro that works both inside and outside a tikzpicture. –  Mar 21 '18 at 04:10
  • @marmot Again, you need to detect the difference - either how tikzmark does it or in some other way. Probably all you need is to tack a toggle switch onto the start of pictures to set a conditional true or false. However, probably there is something you can test already without the need to do this. – cfr Mar 21 '18 at 04:13
  • @cfr: Even after detecting if the macro is called within a tikzpicture how would I define \CircleText without a node? Is a \node within a \node ok? – Peter Grill Mar 21 '18 at 04:16
  • @cfr But I got a working answer. Is there anything wrong with it? –  Mar 21 '18 at 04:24
  • However, putting one node inside another is a no-no, as far as I know. At least, tikzmark supplies a different macro for that case, so you'd need something more analogous to \subnode. – cfr Mar 21 '18 at 04:36
  • @marmot No. That's using the same technique I mentioned: you're using a different macro inside and outside the picture, so there's no nesting. – cfr Mar 21 '18 at 04:39
  • The other thing you might look at is the tikzsymbols package. They can be used even inside nodes inside pictures because they get saved to boxes. (At least, I think that's why.) – cfr Mar 21 '18 at 04:40
  • @cfr I was referring to the above discussion between you and Peter, specifically to the statement "But don't know how else to define a macro to work both within and outside of tikz". This answer does precisely that. –  Mar 21 '18 at 14:14
  • @marmot Er ... yes. You asked if there was anything wrong with it and I said no. I didn't say you couldn't have a macro work both inside and outside: my point was that you can, if that macro calls different macros depending on whether it is inside or outside. That's exactly the approach tikzmark takes and the answer which you got. – cfr Mar 22 '18 at 00:02

1 Answers1

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}
Tarass
  • 16,912
  • 1
    You could use text width={}, see https://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 08: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