10

I want spaced keywords in listings (that is, keyword, with spaces in between).

Now, there are some questions in this site already answered, like this one, where the official answer is that “it is not possible”. But, of course, like almost everything, there are workarounds that let you do it.

However, I've seen in the documentation of microtype correctly highlighted keywords that do contain spaces (e.g., no ligatures, outer kerning, outer spacing).

That's all. If I didn't make myself clear, please, leave a comment. That's the question: I want to reproduce the system used in microtype's documentation.


Update

As @Robert noted, he used latin1 encoding in the documentation. Which seems to work nice with lstlisting (apart from non-breaking spaces, it even accepts, e.g., á, ü by default). But I use utf8 (not for anything special, but because it's recommended everywhere), which doesn't behaves well with listings. Since I like this idea of using non-breaking spaces to define keywords with spaces, I will rewrite my question

Is there a way to teach utf8/listings to understand non-breaking spaces inside of keywords?

Here is a minimal non-working example:

NOTE: the space between spaced and keyword is a non-breaking space (as you can see in the image below).

\documentclass{scrartcl}

%\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenx} % With latin1 it works perfectly

\usepackage{listings}
\lstset{keywords={spaced keyword}, columns=fullflexible}

\begin{document}
\begin{lstlisting}
  notakeyword spaced keyword notakeyword
\end{lstlisting}
\end{document}

enter image description here

Manuel
  • 27,118
  • If you think the question is unclear, feel free to edit ;) – Manuel Jun 16 '14 at 15:45
  • I don't think there is a workaround for this. listings only allows ASCII characters in keywords, and the non-breaking space is an extended-ASCII character (its decimal code is in [128,255]). Highlighting keywords containing ext.-ASCII chars involve that kind of dirty and far-from-optimal tricks: http://tex.stackexchange.com/questions/122850/how-to-insert-a-keyword-with-accents-extendedchars-in-listings – jub0bs Jun 16 '14 at 16:01
  • But then, how does microtype.dtx work? – Manuel Jun 16 '14 at 16:36
  • 1
    The premise of your question is wrong: the file used to generate the microtype manual (microtype.dtx) doesn't use listings at all for typesetting "outer spacing", "outer kerning", "no ligatures" as shown on p17 of that manual. Instead, this is done via a macro called \key defined with \DeclareRobustCommand\key[1]{\textcolor{thered}{\ttfamily#1}} in microtype.dtx. No non-breaking space is involved or necessary for that. – jub0bs Jun 16 '14 at 17:38
  • I'm sorry, but I'm not referring to that (I think). I'm referring to the page 17 on the top, the code shown (I will upload a picture). However, in case I'm still wrong, please, search through the document for non-breaking spaces to see where I'm refering to (again, that might not be a non-breaking space, but I think it is). I will upload a certain part of code of the .dtx (I don't know how dtxes work, so I will upload the raw —commented— code). – Manuel Jun 16 '14 at 18:47
  • 1
    To all of you who voted to close, please, feel free to comment here your doubts or point out what is not clear. – Manuel Jun 16 '14 at 18:49
  • 2
    Now I see what you mean. I think I was looking at the wrong page of the manual. For information, TeX.SX "sanitizes" the text you paste in the answer box; for instance, tabs get converted to 4 spaces. I didn't know it also converts non-breaking spaces to spaces. – jub0bs Jun 16 '14 at 19:09
  • 1
    The author of microtype is a contributor to TeX.SX. I'm sure he's the best person to answer this. – jub0bs Jun 16 '14 at 19:23
  • 1
    That's great. Should I mark it with microtype label to draw his attention? – Manuel Jun 16 '14 at 19:34
  • 1
    Yes, definitely. – jub0bs Jun 16 '14 at 19:48
  • 2
    You need to load inputenc with an encoding that (1) contains the non-breaking space and (2) is not utf8 because listings won't like it. (microtype.dtx uses latin1). – Robert Jun 17 '14 at 23:53
  • 1
    @Robert Well, that's an answer ;) I totally missed that. However, everywhere you look utf8 is recommended (and that's the only reason I use it). (1) Is it possible to teach the non-breaking space to utf8? (2) Is there a workaround so that utf8 won't collide with listings? You can post an answer, but I will wait a little more to see if someone offers a solution. – Manuel Jun 18 '14 at 08:49
  • 1
    @Manuel Yes, you can use linstings plus UTF8. See http://tex.stackexchange.com/questions/176694/non-ascii-character-as-argument/193954#193954 including my first comment about LaTeX usage of encTeX. – wipet Aug 07 '14 at 02:47

1 Answers1

2

Well, most of your questions have already been answered either in the question itself or in the comments, so here's just a recap to satisfy the site's mores:

  • The workaround to allow spaces in keywords that microtype.dtx employs is to use the "non-breaking space" in lieu of a normal space to make listings believe that it's a letter, while it will be output as a space;
  • this requires loading an input encoding that does indeed contain the non-breaking space and maps it to a normal space (e.g., latin1).
  • The problem in your example is that you use utf8 input encoding, which – though it meets the above requirement – is not compatible with the listings package. I don't see a way to fix this incompatibility other than in listings itself.

AFAICT, you only have two options:

  • either don't use utf8 encoding – which in your case appears to be possible considering the fact that you say the only reason why you use it in the first place is that it is recommended everywhere;
  • or use the listingsutf8 package (which understands utf8 input in included files) and store all your code listings in separate files – maybe a bit cumbersome depending on the number of listings you have.
Robert
  • 14,181
  • 1
  • 52
  • 77