我想在同一代码中突出显示 PHP 和 HTML。在尝试过程中,我遇到了三个挑战:
- 似乎 listings 将
<?php
和?>
视为一个大 HTML 标签。如何告诉 listings in<
不是<?php
HTML 标签? - 变量值(这里是 23 和 42)应该有不同的颜色...由于我在(1)中提到的,似乎
<?php
和之间的所有代码都?>
被视为 HTML。 - 变量名...它们应该有自己的颜色。我已经用 修复了这个问题
emph
,但一定有更好的方法吗?
感谢所有帮助!
致以最诚挚的问候 Lars
\documentclass{article}
\usepackage{color}
\usepackage{listings, textcomp}
\definecolor{codecolor}{cmyk}{0.75,0.4,0,0}
\definecolor{codebg}{cmyk}{0.02,0.02,0.02,0.02}
\definecolor{codenumber}{cmyk}{0.2,0.1,0.1,0.2}
\definecolor{codecomment}{cmyk}{0.6,0,1,0}
\definecolor{codestring}{cmyk}{0,1,1,0}
\lstdefinelanguage{myhtml} {
language = HTML,
morecomment = [s]{<!--}{-->},
morecomment = [l]{-->},
otherkeywords = {=, href, target, alt, controls, action, method, src, width, height, type, class}
}
\lstdefinestyle{customphp}{
captionpos = b,
breaklines = true,
breakatwhitespace = true;
xleftmargin = \parindent,
language = PHP,
alsolanguage = myhtml,
upquote = true,
showstringspaces = false,
numbers = left,
basicstyle = \footnotesize\ttfamily\color{black},
commentstyle = \color{codecomment},
keywordstyle = \color{codecolor},
numberstyle = \tiny\color{codenumber},
stringstyle = \color{codestring},
backgroundcolor = \color{codebg},
tagstyle = \color{codecolor},
emph = {$number},
emphstyle = {\color{green}}
}
\begin{document}
\begin{lstlisting}[style=customphp]
<!doctype html>
<html>
<head>
<title> My first PHP document </title>
</head>
<body>
<h1> Welcome to my first PHP document! </h1>
<?php
$number = 23;
echo "Our number is $number. <br>";
$number = 42;
echo "Our number is now $number. <br>";
?>
<p> Some text... </p>
<?php
echo "Some text... <br>";
echo "Our variable is still accessible, our number is $number.";
?>
</body>
</html>
\end{lstlisting}
\end{document}