wg030 / jlcode

A latex package for displaying Julia code using the listings package. The package supports pdftex, luatex and xetex for compilation.
LaTeX Project Public License v1.3c
234 stars 39 forks source link

Emulate github syntax highlighting? #54

Open glwagner opened 1 month ago

glwagner commented 1 month ago

I would like to know if it's possible to emulate github's syntax highlighting, which highlights both functions and literals. For example:

image

from Oceananigans README.

I'm willing to make a contribution to jlcode.sty to achieve this (if its needed, and if its possible) but might need tips.

glwagner commented 1 month ago

This is what the same code looks like in my latex:

image

I'm using

\usepackage[charsperline = 80,
            autoload = true,
            defaultmonofont = false,
            linenumbers = true,
            theme = default]{jlcode}

\definecolor{light-gray}{gray}{0.98}

\lstset{language        = Julia,
        captionpos      = b
    basicstyle      = \ttfamily,
    keywordstyle    = \footnotestyle\ttfamily\bfseries\color{MidnightBlue},
    stringstyle     = \footnotestyle\ttfamily,
        backgroundcolor = \color{light-gray},
        commentstyle    = \footnotestyle\ttfamily\color{Maroon}}

(if that matters)

wg030 commented 1 month ago

I would like to know if it's possible to emulate github's syntax highlighting, which highlights both functions and literals.

Well, I guess the best idea would be to create your own color sheme. It should not be to difficult to do that, just look at the existing ones, see for example https://github.com/wg030/jlcode/blob/master/jlcode.sty#L463-L483 Moreover you would probably need to add a few (or a few more) things in the language definition, see https://github.com/wg030/jlcode/blob/master/jlcode.sty#L119-L370 I would assume that you could get pretty good results with that. If there would sill be things left that don't look the way you want it, you could always try to manually adjust them, see section 8 ("Known Managable Issues") of the manual

glwagner commented 1 month ago

Thank you, that is very helpful! I see the default theme does not distinguish between "base" and functions, eg

https://github.com/wg030/jlcode/blob/9cce985865d2f102936f94dbc5aa05e70197cacf/jlcode.sty#L416-L430

Now I understand that I can use the custom keywords with [1], [2], etc to change these colors from the tex file, ie

\lstset{keywordstyle = {[5]\color{Blue}}}

which I inferred from this:

https://github.com/wg030/jlcode/blob/9cce985865d2f102936f94dbc5aa05e70197cacf/jlcode.sty#L528-L542

Something that seems to be the case (empirically, just from working with it), is that github (or vim) does not simply highlight code based on "tokens", but is rather able to understand how it is used. For example the keyword size is not highlighted by github when it is used as a function keyword, even though this is a julia built-in function:

image

Is this possible with listings (or latex) --- which for example would also allow me to highlight non-julia functions --- anything that has the form func() --- or do I need to settle for manually writing down every token I want highlighted? The latter is ok, just wondering.

Either way this is great, I think I am going to be able to achieve what I need at the end of the day one way or another.

glwagner commented 1 month ago

Does this code:

https://github.com/wg030/jlcode/blob/9cce985865d2f102936f94dbc5aa05e70197cacf/jlcode.sty#L836-L842

mean that we need to define new themes to change colors, rather than using lstset?

wg030 commented 1 month ago

Does this code:

https://github.com/wg030/jlcode/blob/9cce985865d2f102936f94dbc5aa05e70197cacf/jlcode.sty#L836-L842

mean that we need to define new themes to change colors, rather than using lstset?

Yes, the jlcode package was designed in such a way that you would only need to add a new theme and then load it as described in the manual if you wanted to have different colors.

Having that this, instead of doing something like

\lstset{keywordstyle = {[5]\color{Blue}}}

you would only need to set \definecolor{jlfunctions}{HTML}{0000FF} in your new color sheme (unless of course, you would want to highlight some very special functions in yet another color).

Something that seems to be the case (empirically, just from working with it), is that github (or vim) does not simply highlight code based on "tokens", but is rather able to understand how it is used. For example the keyword size is not highlighted by github when it is used as a function keyword, even though this is a julia built-in function:

Yes, github and vim or other code editors are usually way smarter in recognizing code patterns. The detection in listings, which jlcode is completely based on, is rather simple compared to modern code highlighting.

Is this possible with listings (or latex) --- which for example would also allow me to highlight non-julia functions --- anything that has the form func() --- or do I need to settle for manually writing down every token I want highlighted? The latter is ok, just wondering.

Hence something similiar is definitiley not possible with the listings respectively the jlcode package and you could only manually adjust here unfortuantely. I haven't looked for alternatives for years now so that I don't know if somebody has meanwhile come up with a smarter solution than listings as far as code lighting is concerned.

glwagner commented 1 month ago

you would only need to set \definecolor{jlfunctions}{HTML}{0000FF} in your new color sheme (unless of course, you would want to highlight some very special functions in yet another color).

Ok, great!

Hence something similiar is definitiley not possible with the listings respectively the jlcode package and you could only manually adjust here unfortuantely. I haven't looked for alternatives for years now so that I don't know if somebody has meanwhile come up with a smarter solution than listings as far as code lighting is concerned.

This makes sense and thank you for the detailed response! For a paper, its not that crazy to simply write out the extra package-specific functions that we need highlighted anyways.