4

I declare this:

    \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}

Parametrs in \lstset colorize signs in most cases, but it doesn't work on signs: ), {, }. I've tried use for example: \), \_) or \verb=\= - but nothing works. The document compiled but signs are default black.

7
  • 3
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – Skillmon
    Dec 5, 2017 at 13:44
  • Thank You, that's good point :) I hope - I've done that propely. Dec 6, 2017 at 8:06
  • You miss a \begin{document} statement. And you should include a small code example.
    – Skillmon
    Dec 6, 2017 at 8:32
  • 1
    You should not setup keyword colours with the literate keyword. This way listings treats those multi-character keywords as single characters and does only use that much space.
    – Skillmon
    Dec 6, 2017 at 8:48
  • 1
    If you setup keyword colours with the literate keyword, don't always specify 1 as length (that's why they are treated as a single character) but the actual length of the replacement. So for main it would be: \lstset{literate={main}{{\textcolor{red}{main}}}{4}}
    – Skillmon
    Dec 6, 2017 at 9:00

2 Answers 2

2

The problem is the breaklines option you pass to listings. Deep in the code for the implementation of this option the closing parenthesis is used ) internally as a marker in the processing. This happens to interfere with the colouring you attempt to provide.

If you can not drop the breaklines option, then a workaround is to adopt another character for this internal marker. Using etoolbox you can for example make the marking character ~ instead of ) as follows:

\usepackage{etoolbox}
\makeatletter
\patchcmd{\lsthk@SelectCharTable}
  {\lst@ifbreaklines \lst@Def{`)}{\lst@breakProcessOther)}}
  {\lst@ifbreaklines \lst@Def{`~}{\lst@breakProcessOther~}}{}{}
\makeatother

Note there are two occurences of ) that need replacing by the new char ~. Choose a character you do not need to use in the listing.

Here is the patch in operation in a smaller example than your file, but with the code example extended to show line breaking still functions; it also works in your context if you insert the code after loading the listings package, see below.

Sample output

\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}

Here it is in a version of your code. { and } be given colours if you just remember to escape the definition by \ each time:

Sample output

\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}
0

I've used a "hack" and put ")" like as "&)" in code and declarare

{&)}{{{\color{blue})}} 


For example: 
\begin{lstlisting} 
void main(&) 
{ 
} 
\end{lstlisting}

And it works in that way, but i must change ")" to "&)" in every place in code.

But I'm still wainting for other propositions... I want to do that in right way ;)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .