Listings package : Increase spacing between = in ==
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
commentstyle=\color{green}, % comment color
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
commentstyle=\color{green}, % comment color
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
commentstyle=\color{green}, % comment color
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}