2

I'm trying to embed Maxima code into LaTeX using lstlisting and I'm getting extra white space above the listing. This is in my preamble:

\lstset{basicstyle=\ttfamily\color{blue},
breaklines=true,
breakatwhitespace=true,
boxpos=t,
resetmargins=true}

Here's the relevent LaTeX code:

\begin{tabular}{ll}
\textcolor{red}{\ttfamily (\%i1)} &
\begin{lstlisting}
divide(x*((x^2*p^2-t^2)^2 + 2*x*p^3*(x-(2*x^3+2*x^2+x+1)*t)), (x+1)*(t^2-x^2*p^2)^2, t);
\end{lstlisting}
\end{tabular}

and this is the result:

enter image description here

I want the first line of the listing aligned with the top of the table.

Can anybody suggest how to fix this?

2

It would be really nice of you, to present a complete MWE from \documentclass till \end{document}, so we don't have to guess or use the crystal ball.

Nevertheless, I used the ball and modified your example a bit. Please don't blame me for any wrong assumptions.

You are using a table, to present a marker in the left column and the code example in the right column. You enclose the code example in a lstlisting-environment, wich act very similar to a displayed formulae, that is, it adds blank space on top and bottom of your code.

Instead, you want your code to be aligned with the label in the left column, am I right?

To align your code, you have to use the command \lstinline!code! instead of the above environment. Instead of the exclamation mark as delimiter (!), you could choose any other character, that is not present, in the code, you want to present.

Finally, you must convince your table, to peaceful cooperate with \lstinline. I did that by changing the second column into a column of defined width and which is able, to split long lines. To like like an ordinary l-column, it used to be, I added >{\raggedright\arraybackslash} to it. This new columntype was named L (capital L).

This is my example:

\documentclass{article}
\usepackage{listings}
\usepackage{color}

%% New
\usepackage{array}              % to define new column declarations.

%% Define a new column declaration
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}

\lstset{basicstyle=\ttfamily\color{blue},
breaklines=true,
breakatwhitespace=true,
boxpos=t,
resetmargins=true}

\begin{document}
\begin{tabular}{l L{5cm} } % <--- I used the new L-column, made it 5
                           % <--- cm wide!
\textcolor{red}{\ttfamily (\%i1)} &
%% New: use inline listing, it has to be one line!
\lstinline!divide(x*((x^2*p^2-t^2)^2 + 2*x*p^3*(x-(2*x^3+2*x^2+x+1)*t)), (x+1)*(t^2-x^2*p^2)^2, t);!
\end{tabular}
\end{document}

And the result:

enter image description here

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.