3

I used the listings package with single frame and I had an equation before my code. The problem is that the frame intersects a little with the equation and the output is ugly. This happens both when I leave a blank line bewtween the equation and the code and when I am not. How can I fix that? Why did this happenned? enter image description here

Edit I:

\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{unicode-math}

\usepackage{listings}

\begin{document}

$\displaystyle y(t)=\frac{1}{12}-\frac{e^{2t}}{4}+\frac{e^{3t}}{6}$, for $t\ge 0$

\begin{lstlisting}[frame=single,breaklines=true]
clc;
clear all;
close all;
t=0:0.1:20;
y= 1/12 -exp(2*t)/4 + exp(3*t)/6;
G=tf(1,[2 -10 12]);
[y2, t2]=step(G,t);
subplot(2,2,1)
plot(t,y);
subplot(2,2,2)
plot(t2,y2);
\end{lstlisting}

\end{document}
Adam
  • 4,684

2 Answers2

2

You are using \displaystyle for fractions that are in inline math mode. This results in big fractions that extrude below the baseline. Hence this clash.

Without \displaystyle:

enter image description here

They don't clash. Hence if you are using \displaystyle you need some more vertical space and we provide that through aboveskip from listings. Through aboveskip you can adjust the spacing between the listing and the paragraph above it.

Using aboveskip with a suitable dimension

\documentclass[12pt]{article}

\usepackage{amsmath}
\usepackage{unicode-math}

\usepackage{listings}

\begin{document}

$\displaystyle y(t)=\frac{1}{12}-\frac{e^{2t}}{4}+\frac{e^{3t}}{6}$, for $t\ge 0$

\begin{lstlisting}[aboveskip=\baselineskip,frame=single,breaklines=true]
clc;
clear all;
close all;
t=0:0.1:20;
y= 1/12 -exp(2*t)/4 + exp(3*t)/6;
G=tf(1,[2 -10 12]);
[y2, t2]=step(G,t);
subplot(2,2,1)
plot(t,y);
subplot(2,2,2)
plot(t2,y2);
\end{lstlisting}

\end{document}

enter image description here

There is similarly belowskip if you want.

As a side note, since it is matlab code, you may consider Jubobs matlab-prettifier or mcode to typeset these codes. matlab-prettifier has good features. Also refer to this question and its answers.

  • Thank you very useful. What exactly does aboveskip does? Also why is this intersection there in the first place? Shouldn't normally be space between the equation and the frame? – Adam Apr 04 '14 at 13:38
  • 1
    @Adam I have added some explanation. –  Apr 04 '14 at 13:49
  • Shouldn't the baseline change when there is displaystyle in use? And also when there is displaystyle with a table there isn't the same problem although as I understand there should be. – Adam Apr 04 '14 at 14:49
  • @Adam Inline math has limitation of baseline skip. Table is different issue. –  Apr 04 '14 at 14:58
  • @HarishKumar Thanks for the vote of confidence :) – jub0bs Jun 26 '14 at 18:01
  • 1
    @Jubobs Indeed you did good! I myself use matlab-prettifier these days instead of mcode. :) –  Jun 26 '14 at 22:20
1

You could just use a displaymath environment instead. Then there is no need for any adjustments:

enter image description here

Notes:

  • I also added basicstyle=\ttfamily for the listsings. Otherwise, the code, especially G= lined didn't look right.

Code:

\documentclass[12pt]{article}

\usepackage{amsmath} %\usepackage{unicode-math}

\usepackage{listings}

\begin{document} \begin{equation} y(t)=\frac{1}{12}-\frac{e^{2t}}{4}+\frac{e^{3t}}{6}, \text{ for } t\ge 0 \end{equation} % \begin{lstlisting}[frame=single,breaklines=true,basicstyle=\ttfamily] clc; clear all; close all; t=0:0.1:20; y= 1/12 -exp(2t)/4 + exp(3t)/6; G=tf(1,[2 -10 12]); [y2, t2]=step(G,t); subplot(2,2,1) plot(t,y); subplot(2,2,2) plot(t2,y2); \end{lstlisting}

\end{document}

Peter Grill
  • 223,288
  • Thank you. I already use \ttfamily in my code along with some other options for highlighting. Here is just an example. Why without the displaymath environment the G= is like that? – Adam Apr 04 '14 at 13:41