[Tex/LaTex] lstlisting: how to align highlighted code

codecolorfontslistings

I want to define a couple of new commands in order to highlight parts of my code (this is actually for a trace dump, not for a programming language).

My problem is that I can't seem to make the colored text align properly with the listings text. See the "Hello worlds".

I've tried adding \ttfamily to the new command definition but it didn't help. Also tried different font sizes and I believe both colored and non-colored text font are the same size so I don't think that is the problem.

\documentclass{article}

\usepackage{listings}
\lstset{
  basicstyle=\ttfamily\large,
  breakatwhitespace=false,
  breaklines=true,
  escapeinside={\%*}{*},
}

\usepackage{color}
\definecolor{mycolorGreenOne}{rgb}{0, 0.7, 0}
\newcommand*{\listingsHigh}[1]{\textcolor{red}{#1}}
\newcommand*{\LighthighOne}{\color{cyan}}

\begin{document}
\begin{lstlisting}
My text: %*\LighthighOne Hello world!* Goodbye!
My text: Hello world! Goodbye!
My text: %*\listingsHigh{Hello world!}* Goodbye!
\end{lstlisting}
\end{document}

Best Answer

Solved the problem! I added: columns=fullflexible.

I should mention that the description of fullflexible on the documentation seems counterintuitive to me in regards to what is actually doing. This because it appears to indicate that fixed keeps columns "aligned" and flexible "destroys" columns to make sure the original font alignment is respected. However, fixed does not work but fullflexible does. If someone cares to explain why I'd appreciate it.

See section 2.10: listings pdf.

\documentclass{article}

\usepackage{listings}
\lstset{
  basicstyle=\ttfamily\large,
  breakatwhitespace=false,
  breaklines=true,
  escapeinside={\%*}{*},
  columns=fullflexible
}

\usepackage{color}
\definecolor{mycolorGreenOne}{rgb}{0, 0.7, 0}
\newcommand*{\listingsHigh}[1]{\textcolor{red}{#1}}
\newcommand*{\LighthighOne}{\color{cyan}}

\begin{document}
\begin{lstlisting}
My text: %*\LighthighOne Hello world!* Goodbye!
My text: Hello world! Goodbye!
My text: %*\listingsHigh{Hello world!}* Goodbye!
\end{lstlisting}
\end{document}