7

I want to have some text aligned to the center of the top or bottom of an ellipse. I've tried

\documentclass[border=0.5]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mathtools,amsmath}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
        \draw (0,0) ellipse [x radius=3, y radius = 2];
        \draw (0,0) ellipse [red,x radius=4, y radius = 2.66];
        \draw(0,0) ++(-3.5,0)  [decorate,decoration={text along path, text={This text should be between the eppelipses in the bottom half}}] arc [start angle=-160, end angle=-10, x radius=3.5, y radius = 3];
        \draw(0,0) ++(-3.5,0)  [decorate,decoration={text along path, text= {This text should be centered and in the top half}}] arc [start angle=160, end angle=10, x radius=3.5, y radius = 4];
        \node at (0,0) {center text};
\end{tikzpicture}
\end{document}

I get

enter image description here

8

I guess you need to run the arc over the full length and also account for the height of the text. EDIT: Added text align=center, another big thanks to Max!

\documentclass[border=0.5]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{mathtools,amsmath}
\usepackage{tikz}

\usetikzlibrary{decorations.text}
\begin{document}
\begin{tikzpicture}
        \draw (0,0) ellipse [x radius=3, y radius = 2];
        \draw (0,0) ellipse [red,x radius=4, y radius = 2.66];
        \pgfmathsetmacro{\myx}{(3.5cm+height("T")*0.5pt)*1pt/1cm}
        \pgfmathsetmacro{\myy}{(2.33cm+height("T")*0.5pt)*1pt/1cm}
        \draw [decorate,decoration={text along path,text align=center,
         text={This text should be between the ellipses in the bottom half}}] 
        ({-\myx},0) arc [start angle=-180, end angle=00, x radius=\myx, y radius = \myy];
        \pgfmathsetmacro{\myx}{(3.5cm-height("T")*0.5pt)*1pt/1cm}
        \pgfmathsetmacro{\myy}{(2.33cm-height("T")*0.5pt)*1pt/1cm}
        \draw   [decorate,decoration={text along path,text align=center,
         text= {This text
        should be centered and in the top half}}] 
         (180:{\myx} and \myy) arc [start angle=180,
        end angle=0, x radius=\myx, y radius = \myy];
        \node at (0,0) {center text};
\end{tikzpicture}
\end{document}

enter image description here

  • I forgot to adjust the y radius, will do that in a bit. – marmot Aug 28 '18 at 13:04
  • The thing is that I want to center both to the shell, but also such that the center of the text is on the highest/lowest point on the eppelipse – Thorbjørn E. K. Christensen Aug 28 '18 at 13:05
  • @ThorbjørnE.K.Christensen Sorry, need to run now. Either someone else takes over or I'll do in a few hours. – marmot Aug 28 '18 at 13:29
  • No worries ;-) I'm not in a hurry – Thorbjørn E. K. Christensen Aug 28 '18 at 13:30
  • 2
    @ThorbjørnE.K.Christensen You can add text align=center to the decoration options. Also see section 48.6 of the pgf manual, there are a lot of cool options. – Max Aug 28 '18 at 14:49

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.