9

I want to embed a small program listing and have used Insert -> Program Listing in LyX's menu for that.

The settings dialog supports selecting a language (Java), but I can't actually see any difference.

Is there a way to get real syntax highlighting/coloring?

14

Lyx's "Insert -> Program Listing" is internally based on the listings package, which provides a a plenty full of options to influence the typesetting. You can enter such listingsoptions on the "Advanced" page of the "Listing Settings" dialog. For instance, entering

keywordstyle={\color{blue}}

there would typeset all keywords in blue color. For a quick start take a look at other questions with the tag – or consult the (excellent!) listings documentation

The reason you do not "see any difference" in your current setup may be a font issue: The culprit is the standard typewriter font in LaTeX, which does not have a boldface version; however, the default of listings is keywordstyle=\bfseries, that is, keywords are typeset in boldface. So just try another typewriter font in "Document -> Settings -> Fonts" and see if this makes a difference.

(BeraMono is a good font for typesetting listings.)

  • I'm using "Latin Modern Typewriter" is this supposed to work? – soc Sep 15 '11 at 23:55
  • @soc: So have you tried another font to see if this makes a difference? AFAIK LMT supports a boldface series, but at small font sizes the visual difference to normalfont is not too obvious. – Daniel Sep 16 '11 at 7:12
  • Mhhh, I have tried various fonts, but bold worked with none of them... :-/ – soc Sep 16 '11 at 9:43
  • @soc: Have you tried setting keywordstyle explicitly, as described in my answer? – Daniel Sep 16 '11 at 11:49
  • Yes, I set the color to blue, which worked... – soc Sep 16 '11 at 12:20
0

An example for GRASS-GIS module names. The code goes to the Listings section (to be found under Document > Settings... ):

alsoletter={*()"'0123456789.}
alsoother={\{\=\}}
backgroundcolor={\color{lightgray3}}
basicstyle={\small\ttfamily}
breaklines=true
commentstyle={\itshape\color{lightgray}}
fillcolor={\color{red}}
frame=l
framexleftmargin=1em
framextopmargin=1em
keywordstyle={\color{magenta}\bfseries}
language=bash
literate={{=}{{{\color{blue}=}}}1}
morecomment={[n][\keywordstyle]{{}{}}}
morekeywords={GRASS_OVERWRITE, region, zoom, nsres, ewres, MASK, input, in, rast, map, output, out, type, use, attrcolumn, rgbcolumn, labelcolumn, method, size, title, color, null(), S, N, W, E, a, b, c, d, e, f, g, n, s, w, e, --o, -c, -e, -s, -ca}
otherkeywords={g.access,g.cairocomp,g.copy,g.dirseps,g.filename,g.findetc,g.findfile,g.gisenv,g.gui,g.list,g.mapset,g.mapsets,g.message,g.mkfontcap,g.mlist,g.mremove,g.parser,g.pnmcat,g.pnmcomp,g.ppmtopng,g.proj,g.region,g.remove,g.rename,g.setproj,g.tempfile,g.version,r.basins.fill,r.bitpattern,r.buffer,r.carve,r.category,r.circle,r.clump,r.coin,r.colors,r.colors.out,r.composite,r.compress,r.contour,r.cost,r.covar,r.cross,r.describe,r.digit,r.distance,r.drain,r.external,r.external.out,r.fill.dir,r.flow,r.grow,r.grow.distance,r.gwflow,r.his,r.horizon,r.in.arc,r.in.ascii,r.in.bin,r.info,r.in.gdal,r.in.gridatb,r.in.lidar,r.in.mat,r.in.png,r.in.poly,r.in.xyz,r.kappa,r.lake,r.le,r.li,r.los,r.mapcalc,r.mfilter,r.mode,r.neighbors,r.null,r.out.arc,r.out.ascii,r.out.bin,r.out.gdal,r.out.gridatb,r.out.mat,r.out.mpeg,r.out.png,r.out.pov,r.out.ppm,r.out.ppm3,r.out.tiff,r.out.vrml,r.out.vtk,r.param.scale,r.patch,r.profile,r.proj,r.quant,r.quantile,r.random,r.random.cells,r.random.surface,r.reclass,r.recode,r.region,r.regression.line,r.report,r.resamp.bspline,r.resamp.filter,r.resamp.interp,r.resample,r.resamp.rst,r.resamp.stats,r.rescale,r.rescale.eq,r.ros,r.series,r.series.interp,r.shaded.relief2,r.slope.aspect,r.solute.transport,r.spread,r.spreadpath,r.statistics,r.statistics2,r.statistics3,r.stats,r.sun,r.sunmask,r.support,r.support.stats,r.surf.area,r.surf.contour,r.surf.fractal,r.surf.gauss,r.surf.idw,r.surf.idw2,r.surf.random,r.terraflow,r.texture,r.thin,r.timestamp,r.topidx,r.topmodel,r.to.rast3,r.to.rast3elev,r.to.vect,r.transect,r.univar,r.uslek,r.usler,r.viewshed,r.volume,r.walk,r.water.outlet,r.watershed,r.what,r.what.color,v.buffer,v.build,v.build.polylines,v.category,v.class,v.clean,v.colors,v.colors.out,v.convert,v.db.connect,v.db.select,v.delaunay,v.distance,v.drape,v.edit,v.external,v.external.out,v.extract,v.extrude,v.generalize,v.hull,v.in.ascii,v.in.db,v.in.dwg,v.in.dxf,v.info,v.in.lidar,v.in.ogr,v.in.region,v.in.sites,v.kcv,v.kernel,v.label,v.label.sa,v.lidar.correction,v.lidar.edgedetection,v.lidar.growing,v.lrs,v.mapcalc,v.mkgrid,v.neighbors,v.net,v.net.alloc,v.net.allpairs,v.net.bridge,v.net.centrality,v.net.components,v.net.connectivity,v.net.distance,v.net.flow,v.net.iso,v.net.path,v.net.salesman,v.net.spanningtree,v.net.steiner,v.net.timetable,v.net.visibility,v.normal,v.out.ascii,v.out.dxf,v.outlier,v.out.ogr,v.out.postgis,v.out.pov,v.out.svg,v.out.vtk,v.overlay,v.parallel,v.patch,v.perturb,v.proj,v.qcount,v.random,v.reclass,v.rectify,v.sample,v.segment,v.select,v.split,v.support,v.surf.bspline,v.surf.idw,v.surf.rst,v.timestamp,v.to.3d,v.to.db,v.to.points,v.to.rast,v.to.rast3,v.transform,v.type,v.univar,v.vect.stats,v.vol.rst,v.voronoi,v.what,v.what.rast,v.what.rast3,m.cogo,m.measure,m.nviz.image,m.nviz.script,m.transform}
prebreak={\textbackslash}
sensitive=true
stepnumber=1
tabsize=4
upquote=true

It works fine, except for one error: the keyword v.lidar.growing isn't colored correctly. Because there is an r.grow entry already, only a part of it appears to get the desired color (here the bold letters), v.lidar.growing.

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.