1. 程式人生 > >LaTex下的listing,程式碼顯示的高階操作,不加空格對齊,自動換行和外邊框

LaTex下的listing,程式碼顯示的高階操作,不加空格對齊,自動換行和外邊框

近段時間使用latex往裡面添加了一些程式碼,看著是很好看,但是copy出來的程式碼卻一塌糊塗.到處都加滿了空格,還不能自動換行.

S c i e n t i f i c computing f o r Lua .就像這樣,但是在網上也沒有找到相應的解決方法,最後只好到官方的指導文件上去找.官方文件連結:

https://mirrors.sjtug.sjtu.edu.cn/ctan/macros/latex/contrib/listings/listings.pdf

在裡面看了好一段時間終於找到了解決的方法,就是在引入了巨集包之後,新增如下設定.

\lstset{
  breaklines,%自動換行
  columns=flexible,%不隨便新增空格,只在已經有空格的地方新增空格,
%如果想要新增空格使用fixed作為引數(這是預設的),如果堅決不新增空格使用fullflexible作為引數.
}

下面是引數的演示:

程式碼:

\begin{lstlisting}[breaklines=false,columns=fixed,frameshape={RYRYNYYYY}{yny}{yny}{RYRYNYYYY}]
 ______             __   |  Torch7                                 
/_  __/__  ________/ /   |  Scientific computing for Lua.000000000000000000000000000000000000000         
 / / / _ \/ __/ __/ _ \  |                                           
/_/  \___/_/  \__/_//_/  |  https://github.com/torch   
                         |  http://torch.ch    

th>
\end{lstlisting}
\begin{lstlisting}[breaklines,columns=flexible,frameshape={RYRYNYYYY}{yny}{yny}{RYRYNYYYY}]
  ______             __   |  Torch7                                 
 /_  __/__  ________/ /   |  Scientific computing for Lua.000000000000000000000000000000000000000         
  / / / _ \/ __/ __/ _ \  |                                           
 /_/  \___/_/  \__/_//_/  |  https://github.com/torch   
                          |  http://torch.ch    
 
 th>
\end{lstlisting}
 \begin{lstlisting}[breaklines,columns=fullflexible,frameshape={RYRYNYYYY}{yny}{yny}{RYRYNYYYY}]
  ______             __   |  Torch7                                 
 /_  __/__  ________/ /   |  Scientific computing for Lua.         
  / / / _ \/ __/ __/ _ \  |                                           
 /_/  \___/_/  \__/_//_/  |  https://github.com/torch   
                          |  http://torch.ch    
 
 th>
\end{lstlisting}

 

編譯後的效果如下面所示

對比之後可以看到效果.