在 listings 包中包装较长的 Python 数组

在 listings 包中包装较长的 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))

但就像我说的,即使选择了这个选项也breaklines=True无济于事。

问题

有没有什么方法可以直接通过 Latex 而不是通过间接的方式解决这个问题?

答案1

这是一个可以解决问题的方法,尽管它也允许换行逗号:\lstset{literate={,}{,}1}

在此处输入图片描述

\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}

相关内容