/**
listings 包允许设置 basicstyle、keywordstyle、identifierstyle、commentstyle、stringstyle。是否可以为以开头的评论设置不同的样式/*
?
为了让我的问题更清楚。假设我已经设置了环境
\documentclass[a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[svgnames]{xcolor}
\usepackage{listings}
\usepackage[scaled=0.92]{beramono}
\usepackage[scaled=0.92]{berasans}
\usepackage[scaled=0.92]{beraserif}
\lstnewenvironment{MyCode}
{\lstset{
language=C++,
basicstyle=\fontfamily{fvm}\selectfont,
keywordstyle=\bfseries,
identifierstyle=\color{blue},
commentstyle=\fontfamily{fve}\selectfont\color{olive},
stringstyle=\fontfamily{fvs}\selectfont,
frame=single,showstringspaces=false,columns=flexible} }
{}
我有以下代码
\begin{document}
\begin{MyCode}
/*
Multi-line
non-Doxygen comments
*/
#include <iostream>
/** Single-line Doxygen comment */
class MyClass {
private:
int value; /**< After member Doxygen comment */
public:
/**
* Multi-line Doxygen comment
* @param int x new value
* @return bool operation success
*/
bool setValue(int x /**< [in] also Doxygen */) {
this->value = x; /* non-Doxygen comment */
std::cout << "I'm setting value " << x << std::endl;
/* Single line non-Doxygen comment */
return true;
}
};
\end{MyCode}
\end{document}
有混合的:
- 通常以以下开头的评论
/*
- Doxygen(javadoc 注释以
/**
/**
本身以 开头,/*
因此所有评论都按照 格式进行格式化
commentstyle=\fontfamily{fve}\selectfont\color{olive},
是否可以区分这些评论并为/**
评论设置另一种样式?例如,将它们涂成红色而不是橄榄色。
答案1
该listings
包允许使用多个注释分隔符,但只能使用一种注释样式。但是,您可以使用键来绕过此限制moredelim
。在您的特定示例中,您可以编写类似
moredelim = [s][\color{ForestGreen}]{/**}{*/}
\**
并且,用 和 分隔的“注释”所使用的样式*\
将不同于用/*
和分隔的注释所使用的样式*/
。
\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[scaled=0.85]{beramono}
\usepackage{listings}
\usepackage[usenames, dvipsnames]{xcolor}
\lstnewenvironment{MyCode}
{\lstset{
language = C++,
basicstyle = \ttfamily,
keywordstyle = \bfseries,
identifierstyle = \color{blue},
commentstyle = \color{olive},
moredelim = [s][\color{ForestGreen}]{/**}{*/},
stringstyle = \color{magenta},
frame = single,
showstringspaces = false,
columns = flexible}
}{}
\begin{document}
\begin{MyCode}
/*
Multi-line
non-Doxygen comments
*/
#include <iostream>
/** Single-line Doxygen comment */
class MyClass {
private:
int value; /**< After member Doxygen comment */
public:
/**
* Multi-line Doxygen comment
* @param int x new value
* @return bool operation success
*/
bool setValue(int x /**< [in] also Doxygen */) {
this->value = x; /* non-Doxygen comment */
std::cout << "I'm setting value " << x << std::endl;
/* Single line non-Doxygen comment */
return true;
}
};
\end{MyCode}
\end{document}