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.

A problem surfaces when formatting my ten-page long document and I'm going to try to post all the code that relates to the issue, but know that I'm fairly new to LaTeX so I may not realise some parts may affect things and such.

\lstdefinestyle{customc}{
  language=C,
  showstringspaces=false,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=\bfseries\color{green!40!black},
  commentstyle=\itshape\color{purple!40!black},
  identifierstyle=\color{blue},
  stringstyle=\color{orange},
}

\lstset{escapechar=@, style=customc}

\begin{tabular}{p{7cm} l}
\multicolumn{2}{c}{}\\
\hline
This is the TestAndSet atomic operation example implemented in C code. 
It takes a lock and sets it to true. 
The test is the return value of the function. This is usually used
in a while loop. The lock is initialised to false. If it is true, 
it will return true and the loop will not break. &
\begin{lstlisting}[aboveskip=-10pt]
   bool TestAndSet(bool *lock) {
     bool local = *lock;
     *lock = true;
     return local;
   }
\end{lstlisting} \\
\hline
This is the Swap atomic operation example implemented in C code. 
It takes a lock and a key
of type bool. The lock will be initialised to false.
It will then swap the values of these variables, and if the value of
lock was false, the key will now become false, and you can exit the loop 
where the key variable is the loop condition, which was initialised to 
true. &
\begin{lstlisting}
   void Swap(bool *lock, bool *key) {
     bool local = *lock;
     *lock = *key;
     *key = local;
   }
\end{lstlisting} \\
\multicolumn{2}{c}{}
\end{tabular}

I think this is all the code that relates to the problem I have, which is what I will describe now.

Whenever I have those tabulars there, the paragraphs in the first column gets an extra line or something and it looks like they are bottom-aligned instead of top-aligned. It does not happen if I have regular text in column two. And I tried changing the type of the second column to other things with set widths but it keeps doing it. If you want me to post more code then tell me, but the whole document is kind of big, with an output of around 10 pages.

Do you have any idea of what could be wrong with this?

share|improve this question
1  
Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. –  Jubobs Apr 8 at 23:12
1  
Note that if you prepare an Minimal Working Example as Jubobs suggests, you will know whether you've posted all the code relevant to your problem because you will have tested whether that example reproduces it. –  cfr Apr 8 at 23:30

1 Answer 1

up vote 2 down vote accepted

I have polished your code and you placed two columns, p-type and l-type, next to each other. The effect was that left column was lower than you expected when dealing with text. I set them both to p-type, then it was easy to manipulate with them. I enclose an example for your further experiments.

%! latex mal-listings.tex
\documentclass{article}
\pagestyle{empty}
\usepackage{listings}
\usepackage{xcolor}

\begin{document}
\lstdefinestyle{customc}{
  language=C,
  showstringspaces=false,
  basicstyle=\footnotesize\ttfamily,
  keywordstyle=\bfseries\color{green!40!black},
  commentstyle=\itshape\color{purple!40!black},
  identifierstyle=\color{blue},
  stringstyle=\color{orange},
  aboveskip=-0.5\baselineskip,
  }
\lstset{escapechar=@, style=customc}

\begin{tabular}{p{6cm} p{6.5cm}}
%\multicolumn{2}{c}{}\\
\hline
This is the TestAndSet atomic operation example implemented in C code. 
It takes a lock and sets it to true. 
The test is the return value of the function. This is usually used
in a while loop. The lock is initialised to false. If it is true, 
it will return true and the loop will not break. &
%
\begin{lstlisting}
   bool TestAndSet(bool *lock) {
     bool local = *lock;
     *lock = true;
     return local;
   }
\end{lstlisting}
\\ \hline
This is the Swap atomic operation example implemented in C code. 
It takes a lock and a key
of type bool. The lock will be initialised to false.
It will then swap the values of these variables, and if the value of
lock was false, the key will now become false, and you can exit the loop 
where the key variable is the loop condition, which was initialised to 
true. &
\begin{lstlisting}
  void Swap(bool *lock, bool *key) {
     bool local = *lock;
     *lock = *key;
     *key = local;
   }
\end{lstlisting} 
\\ \hline
%\multicolumn{2}{c}{}
\end{tabular}
\end{document}

mwe

share|improve this answer
    
You should be commended for your answer, but keep in mind that new users must be somewhat educated as to how the site works; I always refrain from posting an answer until a proper MWE (if appropriate) has been posted by the OP. If you don't, what will prevent the OP from again not posting an MWE with his/her next question? –  Jubobs Apr 9 at 15:10
    
@Jubobs I don't understand what you mean with MWE and that... Malipivo: I tried that before and it seemed like something was caching between each change of that code so that with each change and rerender/recompile it got worse, making the text in the left column go down more. It would only act normal if I deleted the lstlisting completely, even when I had the right column as a p as well... I find it very hard to debug errors in LaTeX... hmm I'll try this and get back to you! –  Simon Apr 9 at 18:52
1  
MWE is meta.tex.stackexchange.com/questions/228/…, we are/were missing the fully compilable document starting with \documentclass... and ending with \end{document}, it saves time to the solvers to actually start working on the problem. –  Malipivo Apr 9 at 18:56
    
Oh okey. I realised that when I saw your answer, so I understand that. Will think about it until next time! The problem though is that my code works now for some reason... I don't know why. I moved my .tex file to a new location to just safely restart without other related cached files but didn't try it straight away. It feels like the problem in the beginning was related to that somehow because the distance kept increasing when I switch from l to m{} (or something else) as right column, and then back. So as I switched it increased distance, but switching back didn't decrease. –  Simon Apr 9 at 20:03
    
The typeset source code doesn't behave like text with several lines, it is a larger box and therefore affecting vertical placing and spacing differently. We usually wrap boxes into \fbox to see how they exactly fit in the page. –  Malipivo Apr 9 at 20:07

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.