3

I'm writing a report about a website I'm working on to play cards online. I use lstlisting to display my PHP, JavaScript and HTML code. The problem is that I'm using a lot of µ's in my JavaScript codes. For example:

\begin{lstlisting}[caption=some code]
  µ.a('error', µ.m('Server responded: ' + r));
\end{lstlisting}

I use the micro symbol because i wanted to have a shortcut for certain functions.

Now, the problem is that LaTeX won't compile these µ's. As output I get to see .a('error', .m('Server responded: ' + r)); LaTeX simply seems to ignore those µ's. I tried to fix this by using:

\usepackage[utf8x]{inputenc}

but this leads to an error: Package utf8x Error: Character181appearedalone.

Any toughts on how to fix this? Thanks!

lockstep
  • 250,273
Sebastian
  • 33
  • 1
  • 3
  • Is xelatex/lualatex + fontspec an option? If so, all problems should just go away (no inputenc required) – Mikael Öhman Apr 24 '12 at 15:38
  • I'm sorry, I'm relatively new to LaTeX. Are these things just packages or ... ?

    EDIT: I'm using MikTeX, and doesn't that have XeTeX included?

    – Sebastian Apr 24 '12 at 15:48
  • It is possible, I had the same problem displaying \alpha and \beta. I hope I still have the source of this old report, just a sec... Edit: found it, you can use \begin{lstlisting}[escapechar=\#] and then #$\alpha$# or #$\mu$# in your case. – Ailurus Apr 24 '12 at 16:09
  • But then I'd have to change all my appearances of µ in my code, wouldn't I? – Sebastian Apr 24 '12 at 16:18

1 Answers1

8

(pdf)latex solution:

If you use the symbol µ only inside lstlisting you can use the option literate.

Here an example

\documentclass{article}

\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{upgreek}

\begin{document}
\begin{lstlisting}[caption=some code,literate={µ}{$\upmu$}1]
  µ.a('error', µ.m('Server responded: ' + r));
\end{lstlisting}
\end{document}

enter image description here

Marco Daniel
  • 95,681
  • Thanks, this actually does the trick! I already tried something with literate, but that didn't work, but your code does. :) Of course it would be handier to just use µ everywhere in my document, and not only inside lstlisting, but that can be easily fixed with \textmu I assume. – Sebastian Apr 24 '12 at 16:17
  • 2
    @Sebastian: You can also use it inside the document body. Therefor you should have a look at this question: http://tex.stackexchange.com/questions/28031/command-names-with-utf-8-special-characters – Marco Daniel Apr 24 '12 at 16:20
  • For anyone in future that wants to use literate but it may not work: check if you have the correct sign at all places and copy&paste them. I tried the example in the question for myself (did not copy Marco’s answer but wrote the literate myself) and could not get it to work until I copied the whole answer. There is 'MICRO SIGN' (U+00B5) (the one that is used in the question and answer) and then there is 'GREEK SMALL LETTER MU' (U+03BC) that my keyboard layout g – bugybunny Oct 03 '16 at 08:07