Code Highlighting in Latex for Swift

Code highlighting in LaTeX for Swift

Swift works out of the box with minted:

\documentclass{article}
\usepackage{minted}

\begin{document}
\begin{minted}{swift}
let x = 42
println("Hello, \(x)!")
\end{minted}
\end{document}

with xelatex -shell-escape x produces

screenshot

Note, however, that this requires Pygments 2 to be installed.

LaTeX package for syntax highlighting of code in various languages

You can use the listings package. It supports many different languages and there are lots of options for customising the output.

\documentclass{article}
\usepackage{listings}

\begin{document}
\begin{lstlisting}[language=html]
<html>
<head>
<title>Hello</title>
</head>
<body>Hello</body>
</html>
\end{lstlisting}
\end{document}

Objective C code in LATEX listings

You could try the minted package which uses pygments and can give better results than the basic keyword highlighting in listings.

Also see this question

Xcode 8 syntax highlighting doesn't work

Fixed. Problem was related to the presence of target in project which is not compiled. So if you have targets e.g. A, B, C and C is not compiled this cause problems with syntax highlighting.

How to colour lines in LaTeX that match regular expressions

If you just want unadorned diffs, Pygments supports them, so texments (a latex front-end for pygments) does what you want.

But I guess that what you want is to have diff's coloured, while having the syntax of the underlying code highlighted appropriately. This you can't do properly the usual way, in general, because syntax highlighting may depend on the state from the previous line, and with udiffs the previous line may be missing, or an inserted line might follow a deleted line, &c.

To do the right thing, you'd need to syntax highlight the old and new versions, and then scramble the highlighted versions together to get the right output. Quite a bit of work, and I've not heard of anyone who's done that.

You could also try simply modifying the usual syntax highlighter for a language, removing highlighting rules that involve multiline state, and inserting rules to colour lines with udiff markup. Cf. Pygments' Write your own lexer; what you want from diff is trickier, since you want what is coloured to be highlighted, so you can't just make the lines into GenericTokens; I don't know what the right way to do this is.

LaTeX source code listing like in professional books

It seems to me that what you really want, is to customize the look of the captions. This is most easily done using the caption package. For instructions how to use this package, see the manual (PDF). You would probably need to create your own custom caption format, as described in chapter 4 in the manual.

Edit: Tested with MikTex:

\documentclass{report}

\usepackage{color}
\usepackage{xcolor}
\usepackage{listings}

\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox{gray}{\parbox{\textwidth}{#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white,textfont=white}

% This concludes the preamble

\begin{document}

\begin{lstlisting}[label=some-code,caption=Some Code]
public void here() {
goes().the().code()
}
\end{lstlisting}

\end{document}

Result:

Preview

Xcode loses syntax highlighting when file is open in multiple tabs

Just got word from Apple. This is finally fixed in Xcode 10 Beta 5 /p>


Related Topics



Leave a reply



Submit