Search code examples
latextikz

Drawing a circle with a line down its center and text on either side in tikz


I want to create a circle node with a line completely down the center with text on either side of the line. So far the code below draws a bar the same height as the text, but does not extend through the entire center of the circle. How can I accomplish this?

\begin{tikzpicture}
\node (y) [shape=circle,draw] at (0,0) {$\Sigma | f$};
\end{tikzpicture}

Solution

  • There are many ways to do that.

    There is in tikz the notion of multipart nodes and a shape, circle split, that allows to have a dual part in a circle shape. The problem is that only a horizontal splitting is allowed and doing a vertical splitting requires 1/ to rotate the shape to have a vertical split and 2/ to rotate the inner text in the opposite direction.

    \documentclass{article}
    
    \usepackage{tikz}
    \usetikzlibrary{shapes.multipart}
    
    \begin{document}
    \begin{tikzpicture}
    \node [circle split,draw,rotate=90] (y) {\rotatebox{-90}{$\sigma$}
         \nodepart{lower} \rotatebox{-90}{$f$}};
    \end{tikzpicture}
    \end{document}
    

    enter image description here

    But doing that with with regular tikz command is very easy and allows a better control on node appearance, and text alignment.

    I suggest, to first put your text nodes. Then determine the optimal circle size with the fit library. And last to draw the vertical line.

    Here is the corresponding code.

    \documentclass{article}
    
    \usepackage{tikz}
    \usetikzlibrary{fit}
    
    \begin{document}
    \begin{tikzpicture}
    \node[anchor=east,baseline] (sigma) at (0,0) {$\sigma$} ;
    \node[anchor=west,baseline] (f) at (0,0) {$f$} ;
    \node[circle,draw,fit={(sigma)(f)}] (y) {} ;
    \draw (y.south) -- (y.north) ;
    \end{tikzpicture}
    \end{document}
    

    enter image description here

    You can modify the space between the text and the circle with a inner sep parameter on the node y.

    If you need several circles with the same diameter, replace the fit= with a minimum width=1cm (or whatever).

    Last, if you need to have closer nodes, add inner sep=0.5pt (or any other value) to the first two nodes.

    Here is the result with different parameters.

    \begin{tikzpicture}
    \node[anchor=east,baseline,inner sep=0.5pt] (sigma) at (0,0) {$\sigma$} ;
    \node[anchor=west,baseline,inner sep=0.5pt] (f) at (0,0) {$f$} ;
    \node[circle,draw,fit={(sigma)(f)},inner sep=1pt] (y) {} ;
    \draw (y.south) -- (y.north) ;
    \end{tikzpicture}
    

    enter image description here