7

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...

5

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}
|improve this answer|||||
  • Awesome, thanks! I had no idea about this... – mort Jun 9 '14 at 12:57
  • Glad it helps, we all leaned from example like this. – Jesse Jun 9 '14 at 12:58
  • @Jesse Thanks for saving me from a headache. What I found interesting is that I was getting misalignment despite having globally set paragraph indents to 0pt (\setlength{\parindent}{0pt} in the preamble). Does \noindent get redefined or something inside of multicols? – Jonathan Komar Oct 14 '15 at 9:22
3

You don't need multicol:

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

\begin{document}
So why do the two colums not align vertically?
\begin{center}
\hfill
$\!\begin{aligned}[t]
& \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{aligned}$\hfill\hfill
$\!\begin{aligned}[t]
& \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{aligned}$\hfill
\end{center}
\end{document}

enter image description here

Are you really sure about underlining?

|improve this answer|||||
  • Thanks for your answer! However, I think that @Jessi's answer is more readable. Ad underlining: yes, because I need to follow the notation used in the lecture. – mort Jun 9 '14 at 13:45
  • 1
    @mort This one could come handy when the two pseudocodes have different heights. – egreg Jun 9 '14 at 13:46

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.