John Pye wrote:
Hi all
In some Latex documentation for various things, I have seen source code
snippets inserted that show up nicely with syntax highlighting. Is there
any way I can do that with LyX?
So for example, let's say I write an algorithm using some python code. I
want to put the code into a report, and explain it. Where the code
appears in my report, I would like the keywords to appear bold, and the
variables to appear blue, etc, etc -- but I want LyX to sort that all
out automatically.
It seems that the 'highlight' tool will be of use, however I'm not sure
how one can use it:
http://www.andre-simon.de/index.html
This would be a bit like using the 'conversion filters' used for
graphics, but it would need to apply somehow to text. I'd be quite happy
with an ERT way of doing this -- anything really.
Lyx does not have direct support, but yes - there is an ERT way.
\usepackage{listings}
in the preamble, then have a look at
http://www.ctan.org/tex-archive/macros/latex/contrib/listings/listings-1.3.pdf
for details on how you use it.
This package offer:
* syntax higlighting for:
- most programming languages, included
- your own custom languages, if you set that up
* customizable, you can decide what the highlight will look like
* highlighting for code that you type into ERT
* higlighting for complete source code files that you include
without having to type them into lyx. Very convenient, change the
real source and your lyx document changes too. . .
Don't be scared by the 50 pages of pdf documentation, getting
started with python should be as simple as:
\lstset{language=Python}
\begin{lstlisting}
if (a == 5) x = y+3;
else x=17;
\end{lstlisting}
Or:
\lstinputlisting{myfile.py}
to pull in an entire file. It doesn't have to be the entire file,
you can use more arguments to pull out just the lines you want.
Helge Hafting