![Notepad++ 将高亮显示的代码放入 LaTeX 中?](https://linux22.com/image/270778/Notepad%2B%2B%20%E5%B0%86%E9%AB%98%E4%BA%AE%E6%98%BE%E7%A4%BA%E7%9A%84%E4%BB%A3%E7%A0%81%E6%94%BE%E5%85%A5%20LaTeX%20%E4%B8%AD%EF%BC%9F.png)
我想将 Notepad++ 高亮的代码导出到 LaTeX。可以吗?
请注意,我的代码是 PHP 代码。
答案1
考虑lstset
从listings
包中使用。请注意,您不需要全局(或从技术上讲,对当前组全局)的任何选项都可以设置为 的可选参数的一部分\begin{lstlisting}
。
\documentclass{article}
\usepackage{xcolor} % for more colors
\usepackage{listings}
\lstset{
basicstyle=\small\ttfamily,
stringstyle=\color{black!40!white}\ttfamily,
keywordstyle=\color{blue},
commentstyle=\color{green!50!black},
showstringspaces=false,
backgroundcolor=\color{black!5!white}
}
\usepackage{framed} % for contrast
\begin{document}
% Example source from http://php.net/manual/en/function.readdir.php
\begin{framed}
\begin{lstlisting}[language=php]
<?php
if ($handle = opendir('/path/to/files')) {
echo "Directory handle: $handle\n";
echo "Entries:\n";
/* This is the correct way to
loop over the directory. */
while (false !== ($entry = readdir($handle))) {
echo "$entry\n";
}
/* This is the WRONG way to
loop over the directory. */
while ($entry = readdir($handle)) {
echo "$entry\n";
}
closedir($handle);
}
?>
\end{lstlisting}
\end{framed}
\end{document}