2

I wrote a table with code on the left side and an answer box on the right. Also tested with ordinary text on the right. With the answer box inserted, the listing appears to align to the bottom of the box. When I replace it with text that is long enough, it seems like tabular is trying to center each column vertically instead of aligning from the top of each. What I don't understand is that with plain text, that doesn't happen, and with the box that doesn't happen either.

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{listings}

\NewDocumentCommand{\ans}{O{} O{36}}{%
\def\w{#2}%
  \fbox{\makebox(\w pt,18 pt){}}
}

\begin{document}

\begin{tabular}{p{8cm}p{2cm}}
  \begin{lstlisting}[language=c++]
  double sum = 0;
  for (int i = 0; i < n; i++) {
      sum += x[i];    
  }
  \end{lstlisting}
  &
%  \ans[72]
  testing testing 123
  \\
\end{tabular}

\end{document}

1 Answer 1

3

TeX is not aligning the content within its own cell, it is aligning the reference points of the content across all columns. The reference point in the first column is the space added before the listing, the reference point in the second column is the baseline of the first line of text or the centre of the box.

It often helps for non textual material to put a zero space at the top so the reference point is above the box. You can see that in the second block. In the third I move the reference point of the list column a bit by eye.

enter image description here

\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage{listings}

\NewDocumentCommand{\ans}{O{} O{36}}{%
\def\w{#2}%
  \fbox{\makebox(\w pt,18 pt){}}
}

\begin{document}

\begin{tabular}{p{8cm}p{2cm}}
  \begin{lstlisting}[language=c++]
  double sum = 0;
  for (int i = 0; i < n; i++) {
      sum += x[i];    
  }
  \end{lstlisting}
  &
%  \ans[72]
  testing testing 123
  \\
\end{tabular}


\begin{tabular}{p{8cm}p{2cm}}
  \begin{lstlisting}[language=c++]
  double sum = 0;
  for (int i = 0; i < n; i++) {
      sum += x[i];    
  }
  \end{lstlisting}
  &
  \ans[72]
 % testing testing 123
 % \\
\end{tabular}


\bigskip\hrule\bigskip


\begin{tabular}{p{8cm}p{2cm}}
   \vspace{0pt}
  \begin{lstlisting}[language=c++]
  double sum = 0;
  for (int i = 0; i < n; i++) {
      sum += x[i];    
  }
  \end{lstlisting}
  &
    \vspace{0pt}
    % \ans[72]
  testing testing 123
  \\
\end{tabular}


\begin{tabular}{p{8cm}p{2cm}}
  \vspace{0pt}
  \begin{lstlisting}[language=c++]
  double sum = 0;
  for (int i = 0; i < n; i++) {
      sum += x[i];    
  }
  \end{lstlisting}
  &
    \vspace{0pt}
    \ans[72]
 % testing testing 123
 % \\
\end{tabular}


\bigskip\hrule\bigskip


\begin{tabular}{p{8cm}p{2cm}}
   \vspace{-\baselineskip}
  \begin{lstlisting}[language=c++]
  double sum = 0;
  for (int i = 0; i < n; i++) {
      sum += x[i];    
  }
  \end{lstlisting}
  &
    \vspace{0pt}
    % \ans[72]
  testing testing 123
  \\
\end{tabular}


\begin{tabular}{p{8cm}p{2cm}}
   \vspace{-\baselineskip}
  \begin{lstlisting}[language=c++]
  double sum = 0;
  for (int i = 0; i < n; i++) {
      sum += x[i];    
  }
  \end{lstlisting}
  &
    \vspace{0pt}
    \ans[72]
 % testing testing 123
 % \\
\end{tabular}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .