1

I'm trying to integrate C++ code into LaTeX text so that I can visually write explanations of the code or write a specific type of problem. It is very important to understand that I am not trying to get the Program code itself via pdf (I know that everything is already automatically written there), I need it in the text

As you can see, I'm trying to adjust the colors and everything seems to be working, as you can see in the Photo. There is one big problem! C++ libraries are written not in ordinary words, but in <...>. I can write <vector> for example, but I can’t make it completely green. It turns out to make only the vector word itself green, but there are no brackets!

\usepackage{listings}
\usepackage{tcolorbox}

% Set the style for displaying code
\lstset{
    language=C++,
    basicstyle=\ttfamily,
    keywordstyle=\color{blue},
    stringstyle=\color{red},
    commentstyle=\color{green},
    morekeywords={namespace, using, include, for, if, else, return, int, float, double, char, void, bool, struct, class, public, private, protected, const, static, virtual, template, typename, switch, case, default, break, continue, do, while, new, delete, try, catch, throw, inline, typedef, sizeof, enum, extern, volatile, register, union, explicit, friend, operator, this, goto, nullptr, auto, constexpr, decltype},
    keywordstyle=[1]\color{blue},
    morekeywords=[2]{empty, false, top, push_back, reserve, begin, end, size, clear, insert, erase, find, count, lower_bound, upper_bound, front, back, at, assign, swap, emplace, emplace_back, pop_back, pop_front, push_front, capacity, max_size, shrink_to_fit, data, cbegin, cend, crbegin, crend, resize, full, is_empty, is_full},
    keywordstyle=[2]\color{orange},
    morekeywords=[3]{algorithm, array, atomic, bitset, chrono, codecvt, complex, condition_variable, deque, exception, fstream, functional, future, initializer_list, iomanip, ios, iosfwd, iostream, istream, iterator, limits, list, locale, map, memory, mutex, new, numeric, ostream, queue, random, ratio, regex, scoped_allocator, set, sstream, stack, stdexcept, streambuf, string, strstream, system_error, thread, tuple, type_traits, typeindex, typeinfo, unordered_map, unordered_set, utility, valarray,
    vector},
    keywordstyle=[3]\color{DarkGreen},
    literate={\#{\textcolor{blue}{\#}}{1}
    }
}

% Define a new environment for inserting code with a gray background
\newtcbox{\inlinecode}[1][gray!20]{
    on line,
    box align=base,
    colback=#1,
    colframe=#1,
    boxrule=0pt,
    arc=3pt,
    outer arc=3pt,
    leftrule=0pt,
    rightrule=0pt,
    toprule=0pt,
    bottomrule=0pt,
    boxsep=0pt,
    left=2pt,
    right=2pt,
    top=2pt,
    bottom=2pt
}

\newcommand{\code}[1]{\inlinecode{\lstinline{#1}}}

If you thought that you can write these brackets in morekeywords=[3], then you are very mistaken, then you will not be able to get any green color

Very important point: I understand that there are mathematical symbols in C++ that represent greater than and less than. I don't need to make all these <> brackets green, I only need to make them green when denoting C++ libraries

Full code

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1,T2A]{fontenc}
\usepackage[english, russian]{babel}
\usepackage{asymptote}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{indentfirst} % First in the village
\usepackage{tikz}
\usetikzlibrary{graphs,graphs.standard,}
\usepackage{tikz-qtree,tikz-qtree-compat}
\usepackage[left=15mm, top=1cm, right=1.5cm, bottom=10mm, nohead, nofoot]{geometry}
\usepackage{CJKutf8}
\usepackage{amsmath}
\usepackage{array}
\usepackage{colortbl}


%%%%%%%%%%%%%%%%%%%%%% C++ %%%%%%%%%%%%%%%%%%%%%%

% Include a file with settings
\input{cpp_style}

%%%%%%%%%%%%%%%%%%%%%% C++ %%%%%%%%%%%%%%%%%%%%%%

\begin{document}

1. Please don't drag the whole of \code{std} into the global namespace with \code{using namespace std;} - that completely eliminates all the benefits of namespacing and makes your code less predictable.

2. Example of function declaration: \code{void generateSequences()} and \code{int computeValue(int a)}.

3. Using a C++ keyword: \code{if (a > b) \{ std::swap(a, b); \}}

4. Some C++ functions to highlight: \code{empty()}, \code{false}, \code{top()}, \code{push_back()}, \code{reserve()}.

5. Include a library: \code{iostream}, \code{<vector>}, \code{algorithm}.

\end{document}
New contributor
Dmitry is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
12
  • Please provide a FULL minimal working example.
    – NBur
    Commented Jun 11 at 9:35
  • 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 the users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem. Commented Jun 11 at 9:37
  • Added full code
    – Dmitry
    Commented Jun 11 at 9:40
  • 2
    @Dmitry Please make a compilable example which we can copy&paste without having to puzzle it together Commented Jun 11 at 9:50
  • 1
    the link that samcarter gives shows <> brackets colours in a #include <...> rather than a type, but it's the same thing to listings Commented Jun 11 at 10:12

1 Answer 1

1

You can use the trick from https://tex.stackexchange.com/a/409953/36296 to colour your angle brackets:

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[T1,T2A]{fontenc}
\usepackage[english, russian]{babel}
\usepackage{asymptote}
\usepackage{amsfonts}
\usepackage{mathtools}
\usepackage{enumitem}
\usepackage{multicol}
\usepackage[dvipsnames,svgnames]{xcolor}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{indentfirst} % First in the village
\usepackage{tikz}
\usetikzlibrary{graphs,graphs.standard,}
\usepackage{tikz-qtree,tikz-qtree-compat}
\usepackage[left=15mm, top=1cm, right=1.5cm, bottom=10mm, nohead, nofoot]{geometry}
\usepackage{CJKutf8}
\usepackage{amsmath}
\usepackage{array}
\usepackage{colortbl}


%%%%%%%%%%%%%%%%%%%%%% C++ %%%%%%%%%%%%%%%%%%%%%%

\usepackage{listings}
\usepackage{tcolorbox}



% Set the style for displaying code
\lstset{
    language=C++,
    basicstyle=\ttfamily,
    keywordstyle=\color{blue},
    stringstyle=\color{red},
    commentstyle=\color{green},
    morekeywords={namespace, using, include, for, if, else, return, int, float, double, char, void, bool, struct, class, public, private, protected, const, static, virtual, template, typename, switch, case, default, break, continue, do, while, new, delete, try, catch, throw, inline, typedef, sizeof, enum, extern, volatile, register, union, explicit, friend, operator, this, goto, nullptr, auto, constexpr, decltype},
    keywordstyle=[1]\color{blue},
    morekeywords=[2]{empty, false, top, push_back, reserve, begin, end, size, clear, insert, erase, find, count, lower_bound, upper_bound, front, back, at, assign, swap, emplace, emplace_back, pop_back, pop_front, push_front, capacity, max_size, shrink_to_fit, data, cbegin, cend, crbegin, crend, resize, full, is_empty, is_full},
    keywordstyle=[2]\color{orange},
    morekeywords=[3]{algorithm, array, atomic, bitset, chrono, codecvt, complex, condition_variable, deque, exception, fstream, functional, future, initializer_list, iomanip, ios, iosfwd, iostream, istream, iterator, limits, list, locale, map, memory, mutex, new, numeric, ostream, queue, random, ratio, regex, scoped_allocator, set, sstream, stack, stdexcept, streambuf, string, strstream, system_error, thread, tuple, type_traits, typeindex, typeinfo, unordered_map, unordered_set, utility, valarray,
    vector},
    keywordstyle=[3]\color{DarkGreen},
    moredelim=**[s][\coloroncondition]<>
}

\newif\ifcoloranglebrackets
\coloranglebracketsfalse
\newcommand\coloroncondition{\ifcoloranglebrackets\color{DarkGreen}\fi}

% Define a new environment for inserting code with a gray background
\newtcbox{\inlinecode}[1][gray!20]{
    on line,
    box align=base,
    colback=#1,
    colframe=#1,
    boxrule=0pt,
    arc=3pt,
    outer arc=3pt,
    leftrule=0pt,
    rightrule=0pt,
    toprule=0pt,
    bottomrule=0pt,
    boxsep=0pt,
    left=2pt,
    right=2pt,
    top=2pt,
    bottom=2pt,
}

\newcommand{\code}[1]{\inlinecode{\lstinline{#1}}}


%%%%%%%%%%%%%%%%%%%%%% C++ %%%%%%%%%%%%%%%%%%%%%%

\begin{document}

1. Please don't drag the whole of \code{std} into the global namespace with \code{using namespace std;} - that completely eliminates all the benefits of namespacing and makes your code less predictable.

2. Example of function declaration: \code{void generateSequences()} and \code{int computeValue(int a)}.

3. Using a C++ keyword: \code{if (a > b) \{ std::swap(a, b); \}}

4. Some C++ functions to highlight: \code{empty()}, \code{false}, \code{top()}, \code{push_back()}, \code{reserve()}.

5. Include a library: \code{iostream}, {\coloranglebracketstrue\code{<vector>}}, \code{algorithm}.

\end{document}

enter image description here

4
  • This is the first time I've seen such a function for a bracket... Where did you find this anglebracketstrue function?
    – Dmitry
    Commented Jun 11 at 10:37
  • @Dmitry In the question I suggested as duplicate, an action for which you started to throw rude comments at me Commented Jun 11 at 10:39
  • Not rude... I don’t understand why they redirect me to begin if I don’t need it
    – Dmitry
    Commented Jun 11 at 10:41
  • I'd call comments like "You didn’t even understand him and ran faster... " very rude when the linked post very well contained the solution to your problem. Commented Jun 11 at 10:41

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