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程序中自定义<和>之间的颜色,但是程序代码中小于和大于符号以及输入>>、输出符号<<中间的代码也是颜色不正确。
我想要实现这个效果: