2

I am trying to circle some numbers inside tikz enviroment. I followed answers for this question: Good way to make \textcircled numbers?

And I used this solution, which work excellent inside normal text.

\documentclass{article}
\usepackage{tikz}  
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
        \node[shape=circle,draw,inner sep=2pt] (char) {#1};}} 
 \begin{document}
 Numbers aligned with the text:  \circled{1} \circled{2} \circled{3} end.
 \end{document}

Unfortunately I encountered a problem. I want to create a few numbers in circle inside bigger tikz node. But it seems like new "nested" node inherits minimum width from "parent" node so the circle became very huge. Is there any way to work around this?

Thanks for any help.

EDIT:

I added MWE with my problem.

\documentclass{standalone}

\usepackage{tikz}

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
    \node[shape=circle,draw,inner sep=2pt] (char) {#1};}} 

\begin{document}
\begin{tikzpicture}
\tikzstyle{big}=[draw,rectangle,rounded corners, fill=blue!20,minimum width=2cm, minimum height=2cm]

\node[big] (0) {First number = \circled{20},  Second number = \circled{20}};
\end{tikzpicture}
\end{document}

enter image description here

awarus
  • 105
  • Yes, you should never nest tikzpictures. You can store the inner ones in \saveboxes and reuse them inside the outer tikzpicture, though, see https://tex.stackexchange.com/q/47377/121799. –  May 01 '19 at 22:14
  • 1
    `\documentclass{article} \usepackage{tikz}
    \newcommand*\circled[1]{\tikz[baseline=(char.base)]{ \node[shape=circle,draw,inner sep=2pt] (char) {#1};}} \newsavebox\SBoxA
    \newsavebox\SBoxB \newsavebox\SBoxC \begin{document} \savebox\SBoxA{\circled{1}}\savebox\SBoxB{\circled{2}}\savebox\SBoxC{\circled{3}}% Numbers aligned with the text: \circled{1} \circled{2} \circled{3} end.

    \circled{\usebox{\SBoxA} \usebox{\SBoxB} \usebox{\SBoxC}} \end{document}` works.

    –  May 01 '19 at 22:17
  • 1
    Can you please post a fully compilable MWE including \documentclass and the appropriate packages that reproduces the problem in a tikz node. Basic usage \begin{tikzpicture} \node at (0,0) {\circled{1} \circled{2} \circled{3}}; \end{tikzpicture} seems to work, even though it is not recommended. – Peter Grill May 01 '19 at 23:02
  • 1
    @PeterGrill IMHO this is precisely the point: the things that go wrong when nesting tikzpictures are hard to predict, which makes it hard for others to reproduce the issue without knowing what precisely the OP did. And once we know we will be able to to provide a workaround for that specific case, but I am pretty sure that we won't be able to find something that always works, so my recommendation is: do not nest tikzpictures. –  May 01 '19 at 23:21
  • I added MWE with my problem. It is little simplified, but I think it very well demonstrate what kind of problem happends. – awarus May 02 '19 at 07:56
  • For you specific MWE, the \node option big is being inherited ny \circled, so if you change \circled to specify minimum width=1em, minimum height=1em, then you get the desired results. However, please note @marmot's comments. – Peter Grill May 02 '19 at 08:10
  • @PeterGrill thank you, that actually solved my problem (at least temporarily). I keep that comments in mind. – awarus May 02 '19 at 08:20

2 Answers2

3

Do you want something like this?

enter image description here

I have modified the \circled command so that it now accepts a comma separated list. If the list contains only one entry then that entry is circled. If the list contains more than one entry then each entry is circled and then another circle is drawn around all of the circled numbers. So the right-hand entry above is the result of \circled{1,2,3}.

My macro avoids nesting tikzpicture environments by remembering the number of items in the comma separated list. If the comma separated list contains more than than one entry then it uses the tikz fit library to draw the outside circle around all of the nodes. Here is the complete code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{fit}
\newcommand*\circled[1]{%
  \begin{tikzpicture}[baseline=(1.base), 
      circ/.style={shape=circle,draw,inner sep=1pt}]
    \foreach \num [count=\c,remember=\c as \C (initially 0)] in {#1}{
      \node[circ] (\c) at (\c*3ex,0) {\num};% hack the node placement
    }                   % probably should use the positioning library
    \ifnum\C>1\node[circ,fit=(1)(\C)]{};\fi% circle multiple entries
  \end{tikzpicture}%
}

\begin{document}

  Numbers aligned with the text: \circled{1} \circled{2} 
  \circled{3} end and then we have \circled{1,2,3}

\end{document}

I have implicitly assumed that all of the circled entries in the comma separated list are all single characters. If this is not the case then it would be easy to modify the code above so that it uses the tikz positioning library to place the nodes next to each other.

1

The problem in your MWE can be solved by adding minimum size=0pt to the options of the node style in \circled.

\documentclass{standalone}

\usepackage{tikz}

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
    \node[shape=circle,draw,inner sep=2pt,minimum size=0pt] (char) {#1};}} 

\begin{document}
\begin{tikzpicture}
\tikzset{big/.style={draw,rectangle,rounded corners, fill=blue!20,minimum
width=2cm, minimum height=2cm}}

\node[big] (0) {First number = \circled{20},  Second number = \circled{20}};
\end{tikzpicture}
\end{document}

enter image description here

Note that \tikzstyle is deprecated.

However, this is not a clean solution, and it may yield unpredictable results. A clean solution is to put the inner tikz picture in a \savebox.

\documentclass{standalone}

\usepackage{tikz}

\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
    \node[shape=circle,draw,inner sep=2pt,minimum size=0pt] (char) {#1};}} 
\newsavebox\SBoxA   
\begin{document}
\savebox\SBoxA{\circled{20}}%
\begin{tikzpicture}
\tikzset{big/.style={draw,rectangle,rounded corners, fill=blue!20,minimum
width=2cm, minimum height=2cm}}

\node[big] (0) {First number = \usebox\SBoxA,  Second number = \usebox\SBoxA};
\end{tikzpicture}
\end{document}