1

I have been having difficulties to make nodes fit the text in tikz.

What I have done so far:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{arrows, chains, fit, quotes,positioning}


\begin{document}

\begin{tikzpicture}[
node distance = 7mm,
  start chain = A going right,
   txt/.style = {text height=2pt, text depth=2.pt,
                 on chain},
every edge/.append style = {draw, -stealth'}
                        ]

\node [txt] {\phantom{ this is text }};    


\node (f1)  [ fill=blue!20,inner ysep=2.pt, 
        inner xsep=2pt, 
        anchor=text, 
        rectangle, 
        rounded corners=1mm,
             fit=(A-1) (A-1)] {{ This is text}};



    \end{tikzpicture}




\end{document}

, which results in enter image description here

Whereas the desired output should look like: enter image description here , which not only centers the text but also fits the node to the text (i.e. not using a lot of unneeded space).

Any thoughts

gojohob215
  • 11
  • 2
  • You use fit=(A-1) (A-1) in example, and it enlarges the node (f1). Can you explain where does node (A-1) come from? – muzimuzhi Z May 31 '20 at 20:09

1 Answers1

1

Try this:

\documentclass{article}
\usepackage{tikz}
\usepackage{stix2} % use STIX2 font 

\begin{document}
\begin{tikzpicture}
  \node [fill=blue!20, inner sep=3pt, rounded corners=1mm] 
    {This is text};
\end{tikzpicture}
\end{document}

enter image description here

muzimuzhi Z
  • 26,474