Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emulate github syntax highlighting? #54

Open
glwagner opened this issue Sep 27, 2024 · 6 comments
Open

Emulate github syntax highlighting? #54

glwagner opened this issue Sep 27, 2024 · 6 comments

Comments

@glwagner
Copy link

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
Copy link
Author

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
Copy link
Owner

wg030 commented Sep 29, 2024

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
Copy link
Author

glwagner commented Sep 29, 2024

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

jlcode/jlcode.sty

Lines 416 to 430 in 9cce985

\newcommand{\loaddefaultcolors}{%
\definecolor{jlbase}{HTML}{444444}% % julia's base color
\definecolor{jlkeyword}{HTML}{444444}% % julia's keywords
\definecolor{jlliteral}{HTML}{78A960}% % julia's literals
\definecolor{jlbuiltin}{HTML}{397300}% % julia's built-ins
\definecolor{jlmacros}{HTML}{1F7199}% % julia's macros
\definecolor{jlfunctions}{HTML}{444444}% % julia's functions
\definecolor{jlcomment}{HTML}{888888}% % julia's comments
\definecolor{jlstrnum}{HTML}{880000}% % julia's strings or numbers
\definecolor{jlop}{HTML}{444444}% % julia's operators
\definecolor{jreplprompt}{HTML}{066F00}% % for "julia>"
\definecolor{jlbackground}{HTML}{F2F2F2}% % the background of the code block
\definecolor{jlrule}{HTML}{DDDDDD}% % the rule of the code block
\definecolor{jllinenumbers}{HTML}{000000}% % the line numbers of the code block
}

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:

jlcode/jlcode.sty

Lines 528 to 542 in 9cce985

\lstdefinestyle{jlcodeblockstyle}{%
basicstyle={\loadthemecolors\color{jlstrnum}\jlbasicfont},
keywordstyle={[1]\color{jlkeyword}\bfseries},
keywordstyle={[2]\color{jlliteral}},
keywordstyle={[3]\color{jlbuiltin}},
keywordstyle={[4]\color{jlmacros}},
keywordstyle={[5]\color{jlfunctions}},
commentstyle={\color{jlcomment}},
stringstyle={\color{jlstrnum}},
identifierstyle={\color{jlbase}},
showstringspaces=false,
upquote=true,
tabsize=4,
aboveskip={1.5\baselineskip}, belowskip={1.5\baselineskip}
}

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
Copy link
Author

glwagner commented Sep 29, 2024

Does this code:

jlcode/jlcode.sty

Lines 836 to 842 in 9cce985

\newcommand{\ucclit}[1]{%
\ifnum\lst@mode=\lst@Pmode\relax%
{\color{jlliteral}#1}%
\else%
#1%
\fi%
}

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

@wg030
Copy link
Owner

wg030 commented Sep 30, 2024

Does this code:

jlcode/jlcode.sty

Lines 836 to 842 in 9cce985

\newcommand{\ucclit}[1]{%
\ifnum\lst@mode=\lst@Pmode\relax%
{\color{jlliteral}#1}%
\else%
#1%
\fi%
}

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants