內部有 python 程式碼的 2 列佈局 - 重疊問題

內部有 python 程式碼的 2 列佈局 - 重疊問題

我正在使用以下乳膠程式碼來編寫我需要添加 python 程式碼的附錄。

\section{Appendix}
\begin{landscape}
\begin{multicols}{2}
\subsection{Source Code}
\definecolor{keywords}{RGB}{255,0,90}
\definecolor{comments}{RGB}{0,0,113}
\definecolor{red}{RGB}{160,0,0}
\definecolor{green}{RGB}{0,150,0}

 \lstset{language=Python, 
    basicstyle=\ttfamily\small, 
    keywordstyle=\color{keywords},
    commentstyle=\color{comments},
    stringstyle=\color{red},
    showstringspaces=false,
    identifierstyle=\color{green},
    procnamekeys={def,class}}
    %taking python file and adding code here
    \lstinputlisting{code/Logic_K.py}
\end{multicols}
\end{landscape}

程式碼產生的正是我所需要的,因此我有包含 2 列的橫向頁面,且程式碼格式正確。問題是 python 檔案中的一些長行與 Latex 檔案中的第二列重疊。有人可以在這裡幫助我,以便列表功能不會與下一列重疊,而是轉到同一列中的下一行嗎?我會非常樂意提供任何幫助,因為這非常重要。另外,是否可以使列出的程式碼具有較小的字體大小,以便更多的程式碼可以容納在單一頁面上的每一列?

答案1

您必須告訴listings您想要什麼,因此將您的更改lsset

 \lstset{language=Python, 
    basicstyle=\ttfamily\small, 
    keywordstyle=\color{keywords},
    commentstyle=\color{comments},
    stringstyle=\color{red},
    showstringspaces=false,
    identifierstyle=\color{green},
    procnamekeys={def,class},
    breaklines=true,
    postbreak=\raisebox{0ex}[0ex][0ex]{your symbol for line break}}

breaklines將允許listings進行換行,如果發生換行,另一個參數將在新行上列印符號(使用您自己的符號,儘管\hookrightarrow常用)。

編輯

若要解決字體大小問題,請將 for 行替換commentstyle為以下行:

commentstyle=\fontsize{5}{6}\selectfont\color{comments}

答案2

這主要是為了回答你最後一點。無論如何,我在我的論文中使用了拉丁現代語,所以通過把

  basicstyle=\footnotesize\ttfamily\fontseries{lc}\selectfont,

在我的\lstset我可以使用“拉丁現代打字機光凝結”(這裡拉丁現代的一篇很好的文章)。這是我能找到的最窄的等寬字體(免費提供),實際上看起來相當不錯。我使用的行編號\tiny。我沒有使用續行符號,只是breakindent=0.5em

我沒有兩列佈局(並且會在程式碼附錄中避免使用它,但這取決於你是否幸運)。我也排了很長的隊。

相關內容