我郑重声明:
\documentclass[a4paper,12pt,titlepage,headsepline,footsepline,hidelinks]{scrartcl}
\usepackage[margin=2.5cm]{geometry}
\usepackage{ucs}
\usepackage[T1]{fontenc}
\usepackage[polish]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lmodern,scrpage2,lastpage,graphicx,indentfirst,setspace}
\usepackage{hyperref}
\usepackage[owncaptions]{vhistory}
\usepackage{tabularx}
\usepackage{listings}
\usepackage{caption}
\captionsetup{justification=centering}
\usepackage{color} % additional packages
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\definecolor{gainsboro}{HTML}{dcdcdc}
\definecolor{gainsboro2}{HTML}{f8f8f8}
\usepackage{listings}
\lstset{
language=C, % choose the language of the code to colorize code
basicstyle=\fontfamily{pcr}\selectfont\footnotesize\color{red},
keywordstyle=\color{black}\bfseries, % style for keywords
numbers=none, % where to put the line-numbers
numberstyle=\tiny, % the size of the fonts that are used for the line-numbers
backgroundcolor=\color{gainsboro2},
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
tabsize=2, % sets default tabsize to 2 spaces
rulesepcolor=\color{gray},
rulecolor=\color{black},
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
breakatwhitespace=false,
moredelim=**[is][\color{green}]{@}{@},
}
\lstset{
%colorize code
literate={ą}{{\k{a}}}1
{Ą}{{\k{A}}}1
{ę}{{\k{e}}}1
{Ę}{{\k{E}}}1
{ó}{{\'o}}1
{Ó}{{\'O}}1
{ś}{{\'s}}1
{Ś}{{\'S}}1
{ł}{{\l{}}}1
{Ł}{{\L{}}}1
{ż}{{\.z}}1
{Ż}{{\.Z}}1
{ź}{{\'z}}1
{Ź}{{\'Z}}1
{ć}{{\'c}}1
{Ć}{{\'C}}1
{ń}{{\'n}}1
{Ń}{{\'N}}1
{void}{{{\color{red}void}}}1
{main}{{{\color{red}main}}}1
{(}{{{\color{blue}(}}}1
{))}{{{\color{blue})}}}1
{<}{{{\color{blue}<}}}1
{>}{{{\color{blue}>}}}1
{[}{{{\color{blue}[}}}1
{]}{{{\color{blue}]}}}1
{=}{{{\color{blue}=}}}1
{\+}{{{\color{blue}+}}}1
{\-}{{{\color{blue}\-}}}1
{\;}{{{\color{blue};}}}1
}
\lstset{breaklines=true,
basicstyle=\ttfamily}
\lstset{basewidth=0.5em}
\lstset{extendedchars=\true}
\lstset{inputencoding=ansinew}
\begin{document}
Example:
\begin{lstlisting}
uint g;
void Zamien(uint X,Y)
{
uint T;
T=Y;
Y=X;
X=T;
}
void main()
{
g=10;
Zamien(RejestryInt[0],g);
}
\end{lstlisting}
\end{document}
\lstset 中的参数在大多数情况下都会使标志变色,但它对标志不起作用:)
、、{
。}
我试过使用例如:\)
、\_)
或\verb=\=
- 但都不起作用。文档已编译但标志默认为黑色。
答案1
问题在于breaklines
您传递给 的选项listings
。在实现此选项的代码深处,右括号)
在内部用作处理中的标记。这恰好会干扰您尝试提供的颜色。
如果您不能放弃该breaklines
选项,那么一个解决方法是采用另一个字符作为此内部标记。例如,您可以使用以下方式etoolbox
制作标记字符:~
)
\usepackage{etoolbox}
\makeatletter
\patchcmd{\lsthk@SelectCharTable}
{\lst@ifbreaklines \lst@Def{`)}{\lst@breakProcessOther)}}
{\lst@ifbreaklines \lst@Def{`~}{\lst@breakProcessOther~}}{}{}
\makeatother
注意有两个)
需要用新字符替换的字符~
。从列表中选择一个不需要使用的字符。
这是运行中的补丁程序,其示例比您的文件要小,但代码示例已扩展以显示换行仍然有效;如果您在加载包后插入代码,它也可以在您的上下文中工作listings
,请参见下文。
\documentclass{article}
\usepackage{listings,color,etoolbox}
\makeatletter
\patchcmd{\lsthk@SelectCharTable}
{\lst@ifbreaklines \lst@Def{`)}{\lst@breakProcessOther)}}
{\lst@ifbreaklines \lst@Def{`~}{\lst@breakProcessOther~}}{}{}
\makeatother
\lstset{language=C,
literate={(}{{{\color{blue}(}}}1
{)}{{{\color{blue})}}}1
}
\lstset{breaklines=true}
\begin{document}
Example:
\begin{lstlisting}
uint g;
void Zamien(uint X,Y) { uint T; T=Y; Y=X; X=T; }
void main() { g=10; Zamien(RejestryInt[0],g);
long_line_variable = another_long_variable - yet_another_long_variable; }
\end{lstlisting}
\end{document}
这是您的代码版本。 如果您每次都记得逃避定义,就会得到颜色{
:}
\
\documentclass[a4paper,12pt,titlepage,headsepline,footsepline,
hidelinks]{scrartcl}
\usepackage[margin=2.5cm]{geometry}
\usepackage{ucs}
\usepackage[T1]{fontenc}
\usepackage[polish]{babel}
\usepackage[utf8]{inputenc}
\usepackage{lmodern,scrpage2,lastpage,graphicx,indentfirst,setspace}
\usepackage[owncaptions]{vhistory}
\usepackage{tabularx}
\usepackage{caption}
\captionsetup{justification=centering}
\usepackage[usenames,dvipsnames,svgnames,table]{xcolor}
\definecolor{gainsboro}{HTML}{dcdcdc}
\definecolor{gainsboro2}{HTML}{f8f8f8}
\usepackage{listings,etoolbox}
\usepackage{hyperref}
\makeatletter
\tracingpatches
\patchcmd{\lsthk@SelectCharTable}
{\lst@ifbreaklines \lst@Def{`)}{\lst@breakProcessOther)}}
{\lst@ifbreaklines\lst@Def{`~}{\lst@breakProcessOther{~}}}{}{}
\makeatother
\lstset{
language=C, % choose the language of the code to colorize code
basicstyle=\fontfamily{pcr}\selectfont\footnotesize\color{red},
keywordstyle=\color{red}, % style for keywords
numbers=none, % where to put the line-numbers
numberstyle=\tiny, % the size of the fonts that are used for the line-numbers
backgroundcolor=\color{gainsboro2},
showspaces=false, % show spaces adding particular underscores
showstringspaces=false, % underline spaces within strings
showtabs=false, % show tabs within strings adding particular underscores
frame=single, % adds a frame around the code
tabsize=2, % sets default tabsize to 2 spaces
rulesepcolor=\color{gray},
rulecolor=\color{black},
captionpos=b, % sets the caption-position to bottom
breaklines=true, % sets automatic line breaking
breakatwhitespace=false,
moredelim=**[is][\color{green}]{@}{@},
morekeywords={main}}
\lstset{
% colorize code
literate={ą}{{\k{a}}}1
{Ą}{{\k{A}}}1
{ę}{{\k{e}}}1
{Ę}{{\k{E}}}1
{ó}{{\'o}}1
{Ó}{{\'O}}1
{ś}{{\'s}}1
{Ś}{{\'S}}1
{ł}{{\l{}}}1
{Ł}{{\L{}}}1
{ż}{{\.z}}1
{Ż}{{\.Z}}1
{ź}{{\'z}}1
{Ź}{{\'Z}}1
{ć}{{\'c}}1
{Ć}{{\'C}}1
{ń}{{\'n}}1
{Ń}{{\'N}}1
{(}{{{\color{blue}(}}}1
{)}{{{\color{blue})}}}1
{<}{{{\color{blue}<}}}1
{>}{{{\color{blue}>}}}1
{[}{{{\color{blue}[}}}1
{]}{{{\color{blue}]}}}1
{=}{{{\color{blue}=}}}1
{\+}{{{\color{blue}+}}}1
{\-}{{{\color{blue}\-}}}1
{\;}{{{\color{blue};}}}1
{\{}{{{\color{blue}\{}}}1
{\}}{{{\color{blue}\}}}}1
}
\lstset{breaklines=true,
basicstyle=\ttfamily}
\lstset{basewidth=0.5em}
\lstset{extendedchars=\true}
\lstset{inputencoding=ansinew}
\begin{document}
Example:
\begin{lstlisting}
uint g;
void Zamien(uint X,Y) { uint T; T=Y; Y=X; X=T; }
void main() { g=10; Zamien(RejestryInt[0],g); }
\end{lstlisting}
\end{document}
答案2
我使用了一个“黑客”技术,在代码中把“)”当作“&)”,然后声明
{&)}{{{\color{blue})}}
For example:
\begin{lstlisting}
void main(&)
{
}
\end{lstlisting}
它以这种方式工作,但我必须在代码中的每个地方将“)”更改为“&)”。
但我仍在等待其他建议...我想以正确的方式做到这一点;)