Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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}
share|improve this answer
    
Awesome, thanks! I had no idea about this... –  mort Jun 9 at 12:57
    
Glad it helps, we all leaned from example like this. –  Jesse Jun 9 at 12:58

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?

share|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 at 13:45
    
@mort This one could come handy when the two pseudocodes have different heights. –  egreg Jun 9 at 13:46

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.