0

I want to include some code from a Lua script file. It includes some comments including some latex-style INCOMPLETE formulas...

Latex wants to do its magic and tries to insert $ and other stuff, but i cannot tell it, to do nothing.

\documentclass[12pt,paper=a4,]{scrreprt}    

\usepackage{listings}
\lstdefinestyle{mystyle}{%
inputencoding=utf8,
extendedchars=true, % i have some special chars...
mathescape=false,   % i do not like fancy formulas
escapeinside={},    % i do not like to escape something
escapechar={},      % so i have no char defined
texcl=false         % and i do not wanna make fancy comments too..
}
\lstset{style=mystyle}
\lstset{literate=%            % if i add those characters here i got less errors of not recognized characters 
{λ}{{\lambda}}1
{µ}{{\micro}}1
{^}{{\^{}}}1
{²}{{\^{2}}}1
}
\usepackage{listingsutf8}

\begin{document}

\begin{lstlisting}[language={[5.3]Lua}]
     -- n^2-1=6.255+\frac{2.316λ^2}{λ^2-0.6263^2}+\frac{2.765λ^2}{λ^2-32.935^2}
\end{lstlisting}

\end{document}

I want latex to just output those characters as they are. no formating, no specials ... just output it.

erros are: missing $ inserted , extra }, or forgotten$ invalid command in math mode...

The same code using verbatim environment works "perfectly" but it wouldnt solve my problem as this is only a comment of a huge script file. I like to syntax-highlight the rest of it.

  • it would be easier to answer if you fixed your example so people can run it but don't you want λ to generate $\lambda$ as \lambda in text mode will generate errors? You haven't shown what output or what errors you got. – David Carlisle Apr 3 '17 at 14:52
  • i just adapted it to a fully runnable latex source. errors are mentioned below. – zanglerecke Apr 3 '17 at 15:16
0

The error that you get (but did not show in full) is

! Missing $ inserted.
<inserted text> 
                $
l.24      -- n^2-1=6.255+\frac{2.316λ
                                      ^2}{λ^2-0.6263^2}+\frac{2.765λ^2}{λ...

where the line break shows exactly where TeX reached. You defined λ to make the math command \lambda hence the error.

\documentclass[12pt,paper=a4,]{scrreprt}    

\usepackage{listings}
\lstdefinestyle{mystyle}{%
inputencoding=utf8,
extendedchars=true, % i have some special chars...
mathescape=false,   % i do not like fancy formulas
escapeinside={},    % i do not like to escape something
escapechar={},      % so i have no char defined
texcl=false         % and i do not wanna make fancy comments too..
}
\lstset{style=mystyle}
\lstset{literate=%            % if i add those characters here i got less errors of not recognized characters 
{λ}{{$\lambda$}}1
{µ}{{$\micro$}}1
{^}{{\textasciicircum}}1
{²}{{\textasciicircum{2}}}1
}
\usepackage{listingsutf8}

\begin{document}

\begin{lstlisting}[language={[5.3]Lua}]
     -- n^2-1=6.255+\frac{2.316λ^2}{λ^2-0.6263^2}+\frac{2.765λ^2}{λ^2-32.935^2}
\end{lstlisting}

\end{document}
  • thanks, that was the point i missed ;) but it is very .... unusefull, to define every of those special characters – zanglerecke Apr 3 '17 at 15:41
  • alternatively don't treat them special in listings but just define at the document level that λ should be a text (not math) λ using inputenc – David Carlisle Apr 3 '17 at 16:24
0

LaTeX is inserting $ because it recognised you are trying to write in math mode, yet incorrectly. Hence, it tries to correct things for you. Here is an example illustrating the how to in math mode, for the main concerns you have raised in your first edit:

\documentclass{article}
\usepackage[utf8]{inputenc}

\usepackage{natbib}
\usepackage{graphicx}

\title{}
\author{}

\usepackage{amsmath}
\usepackage{siunitx}

\begin{document}

\SI{1.55}{\micro\metre}

more text goes here 

Foo is great, and uses:
$\epsilon_r= n^{2}-\kappa^{2}$

Bar is better, using the expression:
$\frac{2.316\lambda^{2}}{\lambda^{2}-0.6263^{2}}$

\end{document}

which outputs the following:

output1

Regarding the current code you updated:

\documentclass[12pt, paper=a4]{scrreprt}    

\usepackage{listings}
\lstdefinestyle{mystyle}{%
inputencoding=utf8,
extendedchars=true, % i have some special chars...
mathescape=false,   % i do not like fancy formulas
escapeinside={},    % i do not like to escape something
escapechar={},      % so i have no char defined
texcl=false         % and i do not wanna make fancy comments too..
}
\lstset{style=mystyle}
\lstset{literate=%            % if i add those characters here i got less errors of not recognized characters 
{λ}{{$\lambda$}}1
{µ}{{$\micro$}}1
{^}{{\textasciicircum}}1
{²}{{\textasciicircum{2}}}1
}
\usepackage{listingsutf8}

\begin{document}

\begin{lstlisting}[language={[5.3]Lua}]
     n^2-1=6.255+\frac{2.316λ^2}{λ^2-0.6263^2}+
     \frac{2.765λ^2}{λ^2-32.935^2}
\end{lstlisting}

\end{document}

Using LuaLaTeX, and with a line break before the second fraction, this outputs:

output2

As far as I know, it isn't possible to use things outside of math mode unless you write something of the sort:

this circle \( \circ \) is followed by the lambda symbol \( \lambda \)

The output in the first examples becomes:

output3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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