2

I am having problem with lstinline macro. It keeps adding a space after the keyword "Module" - see MWE. How could I convince Latex not to do so?

I already tried with including the "." inside the \koda argument, but then I get 2 spaces onward.

\documentclass{book}
\usepackage{listings} 

\newcommand{\koda}[1]{%
\lstinline[language=[Visual]Basic,keywordstyle=\ttfamily\color{blue}, %
basicstyle=\rmfamily\normalsize, prebreak=, %
morekeywords={Or, Loop, Until, To, As, Single, Module, Double, ByVal}]
{#1}} %

\begin{document}
An example with \koda{Console, Sub et Module}. Continued text ...
\end{document}

Thanks in advance!

1
  • Your MWE is too minimal this time. You need to add \usepackage{xcolor}. Also, you should put curly braces around the language selection, as in \lstinline[language={[Visual]Basic},...], because otherwise there are issues with the parsing of the square brackets. With these fixes, I do not see the space between 'Module' and the '.' that you are talking about. Dec 12, 2012 at 17:52

2 Answers 2

4

If you fix your example by adding curly braces around the language and loading xcolor, there aren't any issues with the spacing after the code. But there is still an issue with spacing before: there's an extra space. I've tried taking out spaces and putting in %'s, and nothing seems to fix it. Also, it seems to be specific to [Visual]Basic; if you try [Visual]C++, the extra space goes away. If you delete the language setting within the \lstinline, and use a \lstset{language=[Visual]Basic} outside the \koda definition, the space also goes away. Maybe a bug somewhere in the Visual Basic definition?

enter image description here

\documentclass{book}
\usepackage{xcolor}
\usepackage{listings} 

\newcommand{\koda}{%
  \lstinline[language={[Visual]Basic},
    keywordstyle=\ttfamily\color{blue},
    basicstyle=\rmfamily\normalsize,
    prebreak=,
    morekeywords={Or, Loop, Until, To, As, Single, Module, Double, ByVal}]}

\begin{document}

An example with LEFT\koda{Console, Sub et Module}RIGHT. Continued text ...

\end{document}
3
  • In this case I would consider to define \koda with no argument, leaving off {#1}, so that one could use all the features of \lstinline such as \koda!text!, avoiding premature tokenization.
    – egreg
    Dec 12, 2012 at 23:28
  • @egreg I agree, that's a better approach. I've edited the answer. Unfortunately, the extra space is still present, and still goes away for language={[Visual]C++}.
    – G. Poore
    Dec 13, 2012 at 1:31
  • Thank you both for your ideas. I used the curly braces around the language definition and left out the argument to \koda. Dec 21, 2012 at 7:48
4

The space before is a bug in lstmisc.sty (2007/02/22 v1.4) in the following "aspect", line 849:

\lst@BeginAspect[keywords,comments]{keywordcomments}
...
\lst@ProvideFamily@@
    \lst@KCAkeywordsB@list\lst@KCAkeywordsB \lst@KC@also
    \lst@gKCAkeywordsB@list\lst@gKCAkeywordsB \lst@KCAWorkB
    {kcb}owo % prefix, other key, working procedure, Output hook
...
\lst@EndAspect

Line 849 should remove the space before the comment:

    {kcb}owo% prefix, other key, working procedure, Output hook

(The next lines without the comment work, because the end of line character 13 (^^M) has catcode 9 for ignore.)

Workaround

If the language is loaded in the preamble, the white space is executed in vertical mode causing no harm:

\lstloadlanguages{[Visual]Basic}
1
  • PLEASE IGNORE THE ABOVE COMMENT. Could not delete it :( I tried fixing the bug and removing the space in lstmisc.sty and it did not help. However, using lstset{...} not straight before lstinline solved the extra space problem.
    – emem
    Sep 14, 2015 at 9:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .