[Tex/LaTex] Multicol: why do columns not align properly

multicolvertical alignment

The following code does not align the contents of the two columns do not align properly; in particular, the content of the left column is a bit lower on the page than the content of the right column.

\documentclass [11pt]{article}
\usepackage{amsmath}
\usepackage{multicol}

\begin{document}
\noindent
So why do the two colums not align vertically?
\begin{multicols}{2}
\begin{align*}
& \text{\underline{function} } f_a(x): \\
& \quad \text{\underline{begin}} \\
& \quad \quad i \leftarrow 1; \\
& \quad \quad \text{\underline{while}} (g(i) \neq x) \text{ \underline{do} }         i \leftarrow i +1; \\
& \quad \quad f_a \leftarrow 0; \\
& \quad \text{\underline{end}}
\end{align*}
\begin{align*}
& \text{\underline{function} } f_a(x): \\
& \quad \text{\underline{begin}} \\
& \quad \quad i \leftarrow 1; \\
& \quad \quad \text{\underline{while}} (g(i) \neq x) \text{ \underline{do} }     i \leftarrow i +1; \\
& \quad \quad f_a \leftarrow 0; \\
& \quad \text{\underline{end}}
\end{align*}
\end{multicols}
\end{document}

Why? And how can I fix this?

Note: I tried using \Columnbreak, to no avail. Curiously, if I add a blank line between the two align* environments, the content of the right column is a bit lower than the one on the left…

Best Answer

They are now. You forgot to insert the \noindent right before the first align or insert a blank line on the second align

enter image description here

Code

\documentclass[11pt]{article}
\usepackage{amsmath}
\usepackage{multicol}

\begin{document}
\noindent    
So why do the two colums not align vertically?
\begin{multicols}{2}
\noindent    %<---- here
\begin{align*}
& \text{\underline{function} } f_a(x): \\
& \quad \text{\underline{begin}} \\
& \quad \quad i \leftarrow 1; \\
& \quad \quad \text{\underline{while}} (g(i) \neq x) \text{ \underline{do} }         i \leftarrow i +1; \\
& \quad \quad f_a \leftarrow 0; \\
& \quad \text{\underline{end}}
\end{align*}   % Or enter a blank line here
\begin{align*}
& \text{\underline{function} } f_a(x): \\
& \quad \text{\underline{begin}} \\
& \quad \quad i \leftarrow 1; \\
& \quad \quad \text{\underline{while}} (g(i) \neq x) \text{ \underline{do} }     i \leftarrow i +1; \\
& \quad \quad f_a \leftarrow 0; \\
& \quad \text{\underline{end}}
\end{align*}
\end{multicols}
\end{document}