# Listings package : Increase spacing between = in ==

Everything is in the title. The minimal example below

\documentclass[a4paper,11pt]{article}

\usepackage{listings}
\usepackage{xcolor}

% set the default code style
\lstset{
frame=tb, % draw a frame at the top and bottom of the code block
tabsize=4, % tab space width
showstringspaces=false, % don't mark spaces in strings
numbers=left, % display line numbers on the left
keywordstyle=\color{blue}, % keyword color
stringstyle=\color{blue!60} % string color
}

\begin{document}
\begin{lstlisting}[language=C++,breaklines]
#include <iostream>
using namespace std;

int main() {
int a = 1;
if ( a == 1 )
cout << "Hello world" << endl;
return 0;
}
\end{lstlisting}

\end{document}


produces the following output

I find the spacing between = in the == operator too small. Therefore I would like to increase it. How can I do that ?

Thanks!

• Welcome to TeX.SX! Have you tried to literate it to add some space? Feb 10, 2017 at 9:17
• What do you mean by literate ? Feb 10, 2017 at 9:20
• It should be possible to \lstset{literate={=}{{=\,}}1}. Feb 10, 2017 at 9:23
• This doesn't work, but I've slightly modify it to \lstset{literate={==}{{=\,=}}1}. The problem is now the spacing between 'a' and '==' is to small :/ Feb 10, 2017 at 9:29
• See my updated answer (code). Feb 10, 2017 at 9:32

You have to increase the “base width”, since = is wider than the default 0.6em. If we do a measurement, we find that 0.6em for the standard font is about 6.57pt, whereas the width of the “=” glyph is about 8.52pt (but it has side bearings). So a base width of 0.8em seems necessary.

\documentclass[a4paper,11pt]{article}

\usepackage{listings}
\usepackage{xcolor}

% set the default code style
\lstset{
frame=tb, % draw a frame at the top and bottom of the code block
tabsize=4, % tab space width
showstringspaces=false, % don't mark spaces in strings
numbers=left, % display line numbers on the left
keywordstyle=\color{blue}, % keyword color
stringstyle=\color{blue!60}, % string color
basewidth=0.8em,
}

\begin{document}

\begin{lstlisting}[language=C++,breaklines]
#include <iostream>
using namespace std;

int main() {
int a = 1;
if ( a == 1 )
cout << "Hello world" << endl;
return 0;
}
\end{lstlisting}

\end{document}


I'd much prefer basicstyle=\ttfamily, though.

\documentclass[a4paper,11pt]{article}

\usepackage{listings}
\usepackage{xcolor}

% set the default code style
\lstset{
frame=tb, % draw a frame at the top and bottom of the code block
tabsize=4, % tab space width
showstringspaces=false, % don't mark spaces in strings
numbers=left, % display line numbers on the left
keywordstyle=\color{blue}, % keyword color
stringstyle=\color{blue!60}, % string color
columns=fullflexible,
basicstyle=\ttfamily,
}

\begin{document}

\begin{lstlisting}[language=C++,breaklines]
#include <iostream>
using namespace std;

int main() {
int a = 1;
if ( a == 1 )
cout << "Hello world" << endl;
return 0;
}
\end{lstlisting}

\end{document}


A possible solution is to use literate as in the following example:

\documentclass[a4paper,11pt]{article}

\usepackage{listings}
\usepackage{xcolor}

% set the default code style
\lstset{
frame=tb, % draw a frame at the top and bottom of the code block
tabsize=4, % tab space width
showstringspaces=false, % don't mark spaces in strings
numbers=left, % display line numbers on the left
keywordstyle=\color{blue}, % keyword color
stringstyle=\color{blue!60}, % string color
literate = { == }{{~=\,=~}}4
}

\begin{document}
\begin{lstlisting}[language=C++,breaklines]
#include <iostream>
using namespace std;

int main() {
int a = 1;
if ( a == 1 )
cout << "Hello world" << endl;
return 0;
}
\end{lstlisting}

\end{document}

• @Renaud This is a solution to only influence that specific group of characters in a very individual way. Probably egreg's more general solution will help you for other chars as well. Feb 10, 2017 at 9:36
• I've saved both solutions in a specific file just in case I have to use it again some day. And I've marked egreg's solution as the most relevant since it's the most general. But once again, thank you! Feb 10, 2017 at 9:41
• @Renaud You're welcome. If you like both solutions consider marking them as useful (pen up). Feb 10, 2017 at 9:43