2

At first I thought that it would split on the half of the number of lines. Then I wrote some code where the number of lines of the first part were more than the one of the second part and it was split exactly where I wanted to. Lucky me? I don't think so. How the multicol splits?

Here is the code I had:

 \documentclass{article}
\usepackage{multicol}
\def\tab{\mbox{\quad}}
\begin{document}
\begin{multicols}{2}
\noindent
READER::\\
$P(mutex_3);$\\
$P(r);$\\
$P(mutex_1);$\\
$readcount++;$\\
if(readcount == 1)\\
\tab P(w);\\
$V(mutex_1);$\\
$V(r);$\\
$V(mutex_3);$\\
read the content of object\\
$P(mutex_1);$\\
$readcount--;$\\
if(readcount == 0)\\
\tab V(w);\\
$V(mutex_1);$\\
WRITER::\\
$P(mutex_2);$\\
writecount++;\\
if(writecount == 1)\\
\tab P(r);\\
$V(mutex_2);$\\
P(w);\\
writing object\\
V(w);\\
$P(mutex_2);$\\
writecount--;\\
if(writecount == 0)\\
\tab V(r);\\
$V(mutex_2);$
\end{multicols}
\end{document}

and I wanted to be separated in the READER and WRITER sections and that's how exactly it was done. Why?

  • Luck? Try adding another line. Are you sure you don't want a tabular environment instead? – egreg Dec 20 '14 at 20:21
  • Really I was lucky @egreg? Well I am fine with that one, so how multicol splits will answer the question. – gsamaras Dec 20 '14 at 20:23
  • Please fix your example so we can see where multicol splits, the fragment above isn't usable on its own and where it splits depends on page size and other factors. Always make example code complete from \documentclassto \end{document} (\\ in a document is almost always wrong and you have lots of them!) – David Carlisle Dec 20 '14 at 23:11
7

multicol is balancing the lines but as always happens if \\ is misused at the end of the paragraph, TeX makes an "all white" line and complains in the log:

Underfull \hbox (badness 10000) in paragraph at lines 6--37

So when multicol balances the columns the first column only has one line more than the second, the last line of the second column is all white.

If you delete the final \\ before the \end{multicols} the warning in the log goes, but now the columns are balanced as

enter image description here

Note unrelated to this math italic should never be used for multi-letter words, the font has wide sidebearings to make adjacent letters look like a product of variables and not a word. So

$readcount--;$\\

should be

$\mathit{readcount}{-}{-};$\\

Although you seem to be varying between math and text mode ?? why is

 if(readcount == 1)

not math? Similarly why is writecount-- set in text mode (where -- is a ligature for an n-dash) but readcount set in math mode with two minus signs?

  • David let me apologize for not editing myself the question. I wasn't here, but I will have your tip in mind for future use. I use \` to change line. Maybe I should use \newline' as stated here personal.ceu.hu/tex/breaking.htm? Thanks for the mathit command, never seen it before (yes I actually a newbie, pretty much). I remember that if -- wasn't in math mode, it would eat the second minus or something. Thanks again! – gsamaras Dec 21 '14 at 16:35
  • @G.Samaras \\ and \newline are the same thing. Probably you should use a dedicated environment for pseudocode eg one of the algorithm packages. If you do it by hand as here at least you need to do it consistently, just looking at the image above shows what happens if the markup is inconsistent, sometimes P sometimes P – David Carlisle Dec 21 '14 at 16:40
  • Yes, algorithm is useful I am suggesting it too, but I wanted to give it a try as I did. Nice answer in the question "How can I explain the meaning of LaTeX to my grandma?" by the way. :) – gsamaras Dec 21 '14 at 16:42

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.