4

NOTE

I have tried the following answer to a similar question , but it doesn't work: lstlisting line wrapping

My problem is trying to put the following code in latex:

import matplotlib.pyplot as plt
import numpy as np

t=[0,30,60,1*60+30,2*60,2*60+30,3*60,3*60+30,4*60,4*60+30,5*60,5*60+30,6*60,6*60+30,7*60,7*60+30,8*60,8*60+30,9*60,9*60+30,10*60]

tai=[40.1,40.1,40.1,40.2,40.3,40.5,40.5,40.6,40.7,40.8,41.0,41.0,41.0,41.0,41.0,41.0,41.0,41.1,41.2,41.2,41.2]

tao=[26.4,26.4,26.4,26.4,26.3,26.3,26.3,26.3,26.2,26.1,26.2,26.1,26.1,26.1,26.1,26.0,26.0,26.0,26.0,26.0,26.0]

twi=[25.7,25.7,25.6,25.5,25.5,25.5,25.5,25.4,25.4,25.4,25.4,25.0,25.0,25.0,25.0,25.0,25.0,25.0,25.1,25.1,25.1]

two=[33.2,32.7,32.4,32.1,31.8,31.7,31.4,31.2,31.0,30.8,30.9,30.8,30.7,30.7,30.6,30.6,30.6,30.4,30.5,30.5,30.4]


## this part of code by Joe Kington###########################
ax = plt.subplot(111) 
fig = plt.figure()
ax = plt.subplot(111)

ax.plot(t,tai,'--',label="air in")
ax.plot(t,tao,'o-',label="air out")
ax.plot(t,twi,label="water in")
ax.plot(t,two,'*-',label="water out")
box= ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xlabel('Time in seconds')
plt.ylabel('Temperature in $^{o}C$')
#plt.show()
######## End of code by Joe Kington #############################


print " \t The Averages \t"
print "The average of the temperature of inflow of air is %f \n"  %(np.mean(tai))

print "The average of the temperature of outflow of air is %f \n"  %(np.mean(tao))

print "The average of the temperature of inflow of water is %f \n"  %(np.mean(twi))

print "The average of the temperature of outflow of water is %f \n"  %(np.mean(two))

but like I said even putting in the option breaklines=True isn't helping.

Question

Is there any way to solve this problem via Latex directly and not via an indirect way?

1 Answers1

5

Here is a something that does the trick, although it also allows for line breaks before commas: \lstset{literate={,}{,}1}.

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage{xcolor}
\usepackage{listings}
\lstset{
    frame=single,
    breaklines=true,
    postbreak=\raisebox{0ex}[0ex][0ex]{\ensuremath{\color{red}\hookrightarrow\space}},
    literate={,}{,}1
}
\begin{document}
\begin{lstlisting}[language=Python]
import matplotlib.pyplot as plt
import numpy as np

t=[0,30,60,1*60+30,2*60,2*60+30,3*60,3*60+30,4*60,4*60+30,5*60,5*60+30,6*60,6*60+30,7*60,7*60+30,8*60,8*60+30,9*60,9*60+30,10*60]

tai=[40.1,40.1,40.1,40.2,40.3,40.5,40.5,40.6,40.7,40.8,41.0,41.0,41.0,41.0,41.0,41.0,41.0,41.1,41.2,41.2,41.2]

tao=[26.4,26.4,26.4,26.4,26.3,26.3,26.3,26.3,26.2,26.1,26.2,26.1,26.1,26.1,26.1,26.0,26.0,26.0,26.0,26.0,26.0]

twi=[25.7,25.7,25.6,25.5,25.5,25.5,25.5,25.4,25.4,25.4,25.4,25.0,25.0,25.0,25.0,25.0,25.0,25.0,25.1,25.1,25.1]

two=[33.2,32.7,32.4,32.1,31.8,31.7,31.4,31.2,31.0,30.8,30.9,30.8,30.7,30.7,30.6,30.6,30.6,30.4,30.5,30.5,30.4]


## this part of code by Joe Kington###########################
ax = plt.subplot(111) 
fig = plt.figure()
ax = plt.subplot(111)

ax.plot(t,tai,'--',label="air in")
ax.plot(t,tao,'o-',label="air out")
ax.plot(t,twi,label="water in")
ax.plot(t,two,'*-',label="water out")
box= ax.get_position()
ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])

# Put a legend to the right of the current axis
ax.legend(loc='center left', bbox_to_anchor=(1, 0.5))
plt.xlabel('Time in seconds')
plt.ylabel('Temperature in $^{o}C$')
#plt.show()
######## End of code by Joe Kington #############################


print " \t The Averages \t"
print "The average of the temperature of inflow of air is %f \n"  %(np.mean(tai))

print "The average of the temperature of outflow of air is %f \n"  %(np.mean(tao))

print "The average of the temperature of inflow of water is %f \n"  %(np.mean(twi))

print "The average of the temperature of outflow of water is %f \n"  %(np.mean(two))
\end{lstlisting}
\end{document}
jub0bs
  • 58,916