7

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
    commentstyle=\color{green}, % comment color
    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

Minimal example showing the == 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? – TeXnician Feb 10 '17 at 9:17
  • What do you mean by literate ? – Renaud Feb 10 '17 at 9:20
  • 1
    It should be possible to \lstset{literate={=}{{=\,}}1}. – TeXnician Feb 10 '17 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 :/ – Renaud Feb 10 '17 at 9:29
  • See my updated answer (code). – TeXnician Feb 10 '17 at 9:32
5

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}

enter image description here

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}

enter image description here

3

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

listing

\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}
  • @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. – TeXnician Feb 10 '17 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! – Renaud Feb 10 '17 at 9:41
  • @Renaud You're welcome. If you like both solutions consider marking them as useful (pen up). – TeXnician Feb 10 '17 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.