5

I am trying to duplicate the node highlighted in green but along a curved path. It mostly works, but when I attempt to apply the text align=center effect to the decoration, I get

Dimension too large.

I get pretty good results if I set \def\Radius{2.8cm}. But, if I set \def\Radius{2.6cm}, then I get the same error as above. This I suspect is due to the fact that the text can no longer fit along the given path.

To avoid this problem in the future, if I can get the text align=center to work, I can provide a \pathwhich is more than sufficient to place the text and draw the \node separately.

enter image description here

As per AndreC's comment, commenting out the fill=yellow and rounded corners=2pt:

\draw [draw=red, 
    postaction={
        decoration={
            text along path,
            text={|+\tiny\scshape|\MyText},
            raise=0.5ex,
            text align=center,% <---- Uncommented
        },
        decorate,
    },
    %% No error with these commented, but results are wack!!
    %fill=yellow,
    %rounded corners=2pt,
    ] 

no longer results in an error. However, with this I get the following:

enter image description here

Notes:

  • The MWE as is produces the image shown above as the text align=center is commented out. To get the railing case, that needs to be uncommented.

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{decorations}
\usetikzlibrary{decorations.text}

\newcommand{\MyText}{ABCDEF}

\tikzset{My Node Style/.style={
        fill=green, 
        draw=red, 
        inner sep=2pt,
        rounded corners=2pt,
        font=\tiny,
}}

\begin{document}
\begin{tikzpicture}

    \node [My Node Style] at (0,2.5) {\textsc{\MyText}};

    \def\Radius{3.0cm}
    \def\StartAngle{100}
    \def\EndAngle{80}
    \def\NodeThickness{1.6ex}

    \draw [draw=red, 
        postaction={
            decoration={
                text along path,
                text={|+\tiny\scshape|\MyText},
                raise=0.5ex,
                %text align=center,% <---- ????? Dimension too big ?????
            },
            decorate,
        },
        fill=yellow,
        rounded corners=2pt,
        ] 

        ([shift={(\StartAngle:\Radius)}]0,0) 
            arc [start angle=\StartAngle, end angle=\EndAngle, radius=\Radius]
            -- ++(\EndAngle:\NodeThickness)
            arc [start angle=\EndAngle, end angle=\StartAngle, radius=\Radius+\NodeThickness]
            -- cycle
            ;
\end{tikzpicture}
\end{document}
  • If you comment on the two lines fill=yellow and rounded corners=2pt, there are no more errors. – AndréC Nov 28 '18 at 20:10
  • You could just measure the width of the text and then adjust \StartAngle and \EndAngle to be such that the text is centered. (Of course, this will make the node smaller.) – marmot Nov 28 '18 at 20:18
  • There is no error with text align=left, but there is one with text align=right, would that be a bug with these key? – AndréC Nov 28 '18 at 20:25
  • @AndréC: Yes, no error, but not quite the results I want. See updated question. – Peter Grill Nov 28 '18 at 20:41
  • @marmot: Yep, that was the comment regarding using \def\Radius{2.8cm}. However, if the text changes in the future it'll take time for me to figure out what is going on, so would prefer a more robust solution. – Peter Grill Nov 28 '18 at 20:42
1

The problem is that your path starts from the bottom left corner, so when you say text align=center you try to center near to the top right corner. But this is not what you want. You want to decorate and center your text only along the bottom (or the top) arc.

So it would be nice if we could make a partial decorate \path ...decorate{...} ...; in an post-action, but we can't.

I can imagine two solutions.

First solution: start drawing your "node" from the middle top arc, in this may the center of the path will be in the middle of the bottom arc.

\documentclass[tikz, border=7pt, convert={density=2100}]{standalone}
\usetikzlibrary{decorations.text}

\newcommand{\MyText}{ABCDEF}

\tikzstyle{My Node Style}=[
  fill=green,
  draw=red,
  inner sep=2pt,
  rounded corners=2pt,
  font=\tiny,
]
\def\Radius{3.0cm}
\def\NodeThickness{1.6ex}
\def\DeltaAngle{10}

\begin{document}
  \begin{tikzpicture}
    \node[My Node Style] at (0,2.5) {\textsc{\MyText}};
    \draw [draw=red,
      postaction={
        decoration={
          text along path,
          text={|+\tiny\scshape|\MyText},
          raise=\NodeThickness/2-0.4ex,
          text align=center,
        },
        decorate,
      },
      fill=yellow,
      rounded corners=2pt,
    ]
      (90:\Radius+\NodeThickness)
      arc [start angle=90, end angle=90+\DeltaAngle, radius=\Radius+\NodeThickness]
      -- ++(90+\DeltaAngle:-\NodeThickness)
      arc [start angle=90+\DeltaAngle, end angle=90-\DeltaAngle, radius=\Radius]
      -- ++(90-\DeltaAngle:\NodeThickness)
      arc [start angle=90-\DeltaAngle, end angle=90, radius=\Radius+\NodeThickness]
    ;
  \end{tikzpicture}
\end{document}

enter image description here

Second solution: You can first draw your "node" and then decorate an arc that goes through the middle of the "node".

\documentclass[tikz, border=7pt, convert={density=2100}]{standalone}
\usetikzlibrary{decorations.text}

\newcommand{\MyText}{ABCDEF}

\tikzstyle{My Node Style}=[
  fill=green,
  draw=red,
  inner sep=2pt,
  rounded corners=2pt,
  font=\tiny,
]
\def\Radius{3.0cm}
\def\NodeThickness{1.6ex}
\def\DeltaAngle{10}

\begin{document}
  \begin{tikzpicture}
    \node[My Node Style] at (0,2.5) {\textsc{\MyText}};
    \draw [draw=red, fill=yellow,rounded corners=2pt,
    ]
      (90+\DeltaAngle:\Radius)
      arc [start angle=90+\DeltaAngle, end angle=90-\DeltaAngle, radius=\Radius]
      -- ++(90-\DeltaAngle:\NodeThickness)
      arc [start angle=90-\DeltaAngle, end angle=90+\DeltaAngle, radius=\Radius+\NodeThickness]
      -- cycle
    ;
    \path[decorate, decoration={
          text along path,
          text={|+\tiny\scshape|\MyText},
          raise=-0.4ex,
          text align=center
        }
    ]
      (90+\DeltaAngle:\Radius+\NodeThickness/2)
      arc [start angle=90+\DeltaAngle, end angle=90-\DeltaAngle, radius=\Radius+\NodeThickness/2]
    ;
  \end{tikzpicture}
\end{document}

Note: In the first solution I removed cycle because it doesn't work well with rounded corners in this case.

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.