Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Problem with space being lost in listings immediately after a literate replacement when using columns=spaceflexible or columns=fullflexible (appears correctly with columns=flexible). A small example follows, with problem being that the space after the replaced operator is lost.

Note: This problem is extremely similar to one posted earlier today, but this time, the problem involves the space after a literate replacement, and the space is lost in both \lstinline and lstlisting modes (previous question involved string replacement, and error occurring only in \lstinline mode).

enter image description here

\documentclass{article}
\usepackage{listings}
\lstset{literate=*{{+}{*}{1}}, columns=fullflexible}

\begin{document}

\noindent
\begin{tabular}{ll}
flexible & \lstinline[columns=flexible]!1 + 1!\    spaceflexible & \lstinline[columns=spaceflexible]!1 + 1!\    fullflexible & \lstinline!1 + 1!\    \end{tabular}

\bigskip
\noindent
Within lstlisting environment with columns=flexible
\begin{lstlisting}[columns=flexible]
1 + 1
\end{lstlisting}

\noindent
Within lstlisting environment with columns=spaceflexible
\begin{lstlisting}[columns=spaceflexible]
1 + 1
\end{lstlisting}

\noindent
Within lstlisting environment with columns=fullflexible
\begin{lstlisting}
1 + 1
\end{lstlisting}

\end{document}
share|improve this question
    
Since you have some responses below that seem to answer your question, please consider marking one of them as ‘Accepted’ by clicking on the tickmark below their vote count (see How do you accept an answer?). This shows which answer helped you most, and it assigns reputation points to the author of the answer (and to you!). It's part of this site's idea to identify good questions and answers through upvotes and acceptance of answers. –  Jubobs Mar 1 at 19:17

1 Answer 1

You can use the option keepspaces. For more details have a look at the documentation.

enter image description here

\documentclass{article}
\usepackage{listings}
\lstset{literate=*{+}{*}{1}, columns=fullflexible,keepspaces}

\begin{document}

\noindent
\begin{tabular}{ll}
flexible & \lstinline[columns=flexible]!1 + 1!\    spaceflexible & \lstinline[columns=spaceflexible]!1 + 1!\    fullflexible & \lstinline!1 + 1!\    \end{tabular}

\bigskip
\noindent
Within lstlisting environment with columns=flexible
\begin{lstlisting}[columns=flexible]
1 + 1
\end{lstlisting}

\noindent
Within lstlisting environment with columns=spaceflexible
\begin{lstlisting}[columns=spaceflexible]
1 + 1
\end{lstlisting}

\noindent
Within lstlisting environment with columns=fullflexible
\begin{lstlisting}
1 + 1
\end{lstlisting}

\end{document}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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