![突出显示列表列表中 < 和 > 之间的字符串](https://linux22.com/image/386898/%E7%AA%81%E5%87%BA%E6%98%BE%E7%A4%BA%E5%88%97%E8%A1%A8%E5%88%97%E8%A1%A8%E4%B8%AD%20%3C%20%E5%92%8C%20%3E%20%E4%B9%8B%E9%97%B4%E7%9A%84%E5%AD%97%E7%AC%A6%E4%B8%B2.png)
listings 包提供了 moredelim 选项,允许为给定分隔符括起来的单词设置自定义样式。我想为 <...> 设置自定义颜色,但效果似乎不太好。MWE:
\documentclass{standalone}
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}
\usepackage[listings,skins,breakable]{tcolorbox}
\definecolor{darkred}{RGB}{139,0,0}
\definecolor{darkblue}{RGB}{0,0,139}
\definecolor{darkgreen}{RGB}{0,128,0}
\definecolor{lightgray}{RGB}{238,239,240}
\definecolor{header}{RGB}{163,21,21}
\definecolor{hinclude}{RGB}{128,128,128}
\lstdefinestyle{mystyle}{
language=C++,
extendedchars=true,
breaklines=true,
breakatwhitespace=true,
basicstyle=\ttfamily,
keywordstyle=\color{darkblue},
keywordstyle=[2]\color{blue},
keywordstyle=[3]\color{darkblue},
keywordstyle=[4]\color{darkgreen},
alsoletter = {!},
tabsize=2,
moredelim = [s][\color{header}]{\#}{>},
literate=
*{\#include}{{\textcolor{hinclude}{\#include}}}{8}
{>}{{\textcolor{header}{>}}}{1},
moredelim = [s][\color{header}]{<}{>},
keywords=[2]{cout,cin},
}
\newtcblisting{code}{
arc=0mm,
top=0mm,
bottom=0mm,
left=3mm,
right=0mm,
width=\textwidth,
boxrule=1pt,
colback=lightgray,
listing only,
listing options={style=mystyle},
breakable
}
\begin{document}
\begin{code}
#include<vector>
#include<iostream>
#include<conio.h>
using namespace std;
int bfs(int startNode, int endNode)
{
memset(parentsList, -1, sizeof(parentsList));
memset(currentPathCapacity, 0, sizeof(currentPathCapacity));
queue<int> q;
while (!q.empty())
{
int currentNode = q.front();
q.pop();
for (int i = 0; i < graph[currentNode].size(); i++)
{
int to = graph[currentNode][i];
if (parentsList[to] == -1)
{
if (capacities[currentNode][to] - flowPassed[currentNode][to] > 0)
{
}
cout << "enter the number of nodes and edges\n";
cin >> nodesCount >> edgesCount;
for (int edge = 0; edge < edgesCount; edge++)
{
cout << "enter the start and end vertex alongwith capacity\n";
int from, to, capacity;
cin >> from >> to >> capacity;
}
}
\end{code}
\end{document}
我想在头文件包含的c程序中自定义<和>之间的颜色,但是程序代码中小于和大于符号以及输入>>、输出符号<<中间的代码也是颜色不正确。
我想要实现这个效果: