
我有一些 Java 原始碼,想在 LaTex 中顯示它:
\lstset{language=Java}
\begin{lstlisting}
IntentFileter intentfilter = new IntentFilter();
intentfilter.addAction("com.anguanjia.action.download.START_DOWNLOAD");
intentfilter.addAction("com.anguanjia.action.download.DOWNLOAD_SUCCESS");
g = new blm(this, null);
m.registerReceiver(g, intentfilter);
\end{lstlisting}
但輸出確實很難看:
是我做錯了什麼還是這就是應該的?
答案1
這可能是拖延和美化的起點:
\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\lstset{language=Java,
basicstyle=\footnotesize\ttfamily,
commentstyle=\ttfamily\itshape\color{gray},
stringstyle=\ttfamily,
showstringspaces=false,
breaklines=true,
frameround=ffff,
frame=single,
rulecolor=\color{black},
tabsize=1,
keywordstyle=\color{red}\bfseries,
columns=fullflexible,
morekeywords={public, class}
}
\begin{document}
\begin{lstlisting}
IntentFileter intentfilter = new IntentFilter();
intentfilter.addAction("com.anguanjia.action.download.START_DOWNLOAD");
intentfilter.addAction("com.anguanjia.action.download.DOWNLOAD_SUCCESS");
g = new blm(this, null);
m.registerReceiver(g, intentfilter);
\end{lstlisting}
\end{document}
您可以使用內部指令進一步自訂\lstset
。詳細資訊請參閱listings
手冊。如需極緻美化,您可以使用tcolobox
它的listings only
選項。tcblisting
在tcolorbox
文件中搜尋。