# Causal diagram: Aligning and downscaling shapes in TikZ

I need help to center X and Y outlined in red in the image attached. I also seek help to downscale the Y shape if possible.

Code below:

\documentclass[margin=10pt]{article}
\usepackage{tikz-cd}
\usetikzlibrary{shapes.geometric}
\usepackage{mathrsfs}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\begin{document}
\begin{tikzcd}[cells={nodes={%
,ellipse
,minimum width=1.7cm
,minimum height=1.7cm
,draw
,align=center
}}
,every arrow/.append style={-LaTeX, >=LaTeX}
,row sep=2cm
,column sep=1.2cm
]
& & |[alias=X]|X &  \\
& |[alias=1]|1 \arrow[right]{Y}{?} \arrow[right]{X}{?} & |[alias=2]|2 \arrow[right]{Y}{?} \arrow[right]{X}{?} & |[alias=3]|3 \arrow[right]{Y}{?} \arrow[right]{X}{?} & |[alias=4]|4 \arrow[right]{Y}{?} \arrow[right]{X}{?}  \\
&  & |[alias=Y]|Y
\end{tikzcd}
\end{document}


Output:

• Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. And thanks for the working MWE! Commented Jun 12, 2017 at 11:34

Omit align=center from nodes options, since it seems to be related to the alignment (with it it is typeset in text instead of in math mode). Also 1, 2, 3, and 4 are misaligned.

If you want an upright X use \mathrm{X}.

To reduce the dimension of a circle, reduce minimum width and minimum height only for that cell. Or better, use circle instead of ellipse and play with minimum width only.

MWE:

\documentclass[margin=10pt]{article}
\usepackage{tikz-cd}
\usetikzlibrary{shapes.geometric}
\usepackage{mathrsfs}
\usepackage{amsmath}
\usetikzlibrary{arrows,positioning}

\begin{document}
\begin{tikzcd}[cells={nodes={%
,circle
,minimum width=1.7cm
,draw
}}
,every arrow/.append style={-LaTeX}
,row sep=2cm
,column sep=1.2cm
]%
& & |[alias=X]|\mathrm{X} &  \\
& |[alias=1]|1 \arrow[right]{Y}{?} \arrow[right]{X}{?}
& |[alias=2]|2 \arrow[right]{Y}{?} \arrow[right]{X}{?}
& |[alias=3]|3 \arrow[right]{Y}{?} \arrow[right]{X}{?}
& |[alias=4]|4 \arrow[right]{Y}{?} \arrow[right]{X}{?}  \\
&  & |[alias=Y,minimum width=1cm]|\mathrm{Y}
\end{tikzcd}
\end{document}


Output:

## EDIT

I'm sorry, but I didn't understand completely your requests...

You wrote "I need help to center X and Y" and I thought you were referring to X and Y inside the circles.

The correct way to center the X and Y circles in the diagram is the one described in CarLaTeX's answer, however the right amount is -1.45cm calculated by

- (minimum width + column sep) / 2 = - (1.7 + 1.2) / 2 = -1.45 cm


So, the MWE:

\documentclass[margin=10pt]{article}
\usepackage{tikz-cd}
\usetikzlibrary{shapes.geometric}
\usepackage{mathrsfs}
\usepackage{amsmath}
\usetikzlibrary{arrows,positioning}

\begin{document}
\noindent\begin{tikzcd}[cells={nodes={%
,circle
,minimum width=1.7cm
,draw
}}
,every arrow/.append style={-LaTeX}
,row sep=2cm
,column sep=1.2cm
]%
& & &[-1.45cm] |[alias=X]|\mathrm{X} &[-1.45cm]  \\
& |[alias=1]|1 \arrow[right]{Y}{?} \arrow[right]{X}{?}
& |[alias=2]|2 \arrow[right]{Y}{?} \arrow[right]{X}{?}
& & |[alias=3]|3 \arrow[right]{Y}{?} \arrow[right]{X}{?}
& |[alias=4]|4 \arrow[right]{Y}{?} \arrow[right]{X}{?}  \\
& & & |[alias=Y,minimum width=1cm]|\mathrm{Y}
\end{tikzcd}
\end{document}


produces what you want:

• I'm sorry my question was not adequately formulated. However, your answer helped me a long way. :-) (second-day-user)
– mie
Commented Jun 13, 2017 at 16:55

I must confess I don't know how to work on the positioning tikzcd, but you can do the same picture in pure tikz like this:

\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning,calc}
\begin{document}
\begin{tikzpicture}[
State/.style={draw,circle,minimum width=1.7cm,inner sep=0pt},
>={Latex[scale=1.2]},
node distance=2cm and 1.2cm
]
\node[State] (s1){1};
\node[State,right=of s1] (s2){2};
\node[State,right=of s2] (s3){3};
\node[State,right=of s3] (s4){4};
\node[State,above=of {$(s2.north)!0.5!(s3.north)$}] (sx){X};
\node[State,below=of {$(s2.south)!0.5!(s3.south)$},minimum width=1cm] (sy){Y};
%%
\foreach \num in {1,2,3,4}{
\draw[->] (s\num)--(sx)node[pos=0.5,left=2mm]{?};
\draw[->] (s\num)--(sy)node[pos=0.5,right=2mm]{?};
}
\end{tikzpicture}
\end{document}


Adding a "fake" column and playing with the column width with &[...] you could get StefanH's output with karlkoeller's solution.

Edit (thanks to karlkoeller): the correct width adjustment is

-(column sep width + minimun width)/2 = -(1.2cm + 1.7cm)/2 = -1.45cm


MWE:

\documentclass[margin=10pt]{article}
\usepackage{tikz-cd}
\usetikzlibrary{shapes.geometric}
\usepackage{mathrsfs}
\usepackage{amsmath}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}

\begin{document}
\begin{tikzcd}[cells={nodes={%
,ellipse
,minimum width=1.7cm
,minimum height=1.7cm
,draw
}}
,every arrow/.append style={-LaTeX, >=LaTeX}
,row sep=2cm
,column sep=1.2cm
]%
& & &[-1.45cm] |[alias=X]|\mathrm{X} &[-1.45cm]  \\
& |[alias=1]|1 \arrow[right]{Y}{?} \arrow[right]{X}{?}
& |[alias=2]|2 \arrow[right]{Y}{?} \arrow[right]{X}{?}
& & |[alias=3]|3 \arrow[right]{Y}{?} \arrow[right]{X}{?}
& |[alias=4]|4 \arrow[right]{Y}{?} \arrow[right]{X}{?}  \\
& & & |[alias=Y,minimum width=1cm,minimum height=1cm]|\mathrm{Y}
\end{tikzcd}
\end{document}


Output:

• +1 I misunderstood the question... The right amount is -1.45cm, however. See the edit in my answer. Commented Jun 13, 2017 at 5:31
• @mie (the OP): please accept this answer rather than mine, if you're willing to use this solution. Commented Jun 13, 2017 at 5:33
• @karlkoeller I've edited my answer (before reading the edit of yours LOL, but the calculation is the same). However, thank you, I took into account the column sep but I forgot the column width. Commented Jun 13, 2017 at 6:02
• @karlkoeller: I had actually given up on this part for the centered text inside the shapes. Thank you for this knowledge. All of my diagrams have been updated based on the code you wrote. :-)
– mie
Commented Jun 13, 2017 at 16:49
• @CarLaTeX: Thank you for taking your time. Question fully answered! :-)
– mie
Commented Jun 13, 2017 at 16:50