Repository navigation
Description
Description
Right now, Typst support predefined languages (and their syntax highlighting styles) within raw code block like Markdown in GitHub, Stack Overflow etc. But I want to be able to only type the name (relative/absolute path) of the source code file in the Typst file and have an output similar to using minted with LaTeX.
example.tex:
\documentclass{article}
\usepackage{minted}
\begin{document}
\inputminted{latex}{\jobname.tex}
\end{document}pdflatex -shell-escape example:
This should probably go to a separate issue, but I wanted to include it here anyway. I always use tcolorbox package as a wrapper for minted by using a custom TCPInputListing macro that gives a nice frame for the code and an optional title (and other config settings):
\documentclass{article}
\usepackage[margin=2cm]{geometry}
\usepackage{fontspec}
\setmonofont[Scale=0.9]{Liberation Mono}
\usepackage[cachedir=.minted_cache]{minted}
\usepackage{dingbat} % For minted breaklines character (goes before amssymb)
\usemintedstyle[latex]{emacs}
\usepackage[most]{tcolorbox}
\tcbuselibrary{minted}
\newcommand\codesize{\fontsize{10}{10}\selectfont}
\DeclareTCBInputListing{\includeCodeFile}{m m !O{} !O{} !+O{}}{
minted language=#1,
listing file=#2,
minted options={
fontsize=\codesize,
breaksymbolleft=\raisebox{0.8ex}{\small\reflectbox{\carriagereturn}},
breaksymbolright=\small\carriagereturn,#4
},
enhanced,
listing only,
breakable,
colframe=black,
colback=white,
top=1pt,
bottom=1pt,
left=1pt,
right=1pt,
boxrule=1pt,
IfBlankF={#3}{
top=4ex,
title={\codesize\Verb|#3|},
attach boxed title to top left={yshift=-\tcboxedtitleheight},
boxed title style={
sharp corners=uphill,
top=1pt, bottom=1pt, left=1pt, right=1pt,
boxrule=1pt
},
coltitle=black,
colbacktitle=white,
},
#5
}
\begin{document}
\includeCodeFile{latex}{\jobname.tex}[\jobname.tex]
\end{document}lualatex -shell-escape example:
Use Case
This feature is useful when you want to provide a code example of any sort in your document, but you either don't want to bloat the Typst file or you want to edit the source code file so that the output document will also be changed accordingly. I use latexmk -pvc -lualatex -interaction=nonstopmode -shell-escape to watch for not only LaTeX file changes, but also for any source code files included via minted. If I have 20 source code files included in my document, and I'm currently watching for changes, I can quickly edit something in the source code in my IDE. When I'm done, the updated PDF file will already contain the latest changes I introduced to the source code (kind of don't have to wait for it to recompile).
Manually copy+pasting all the source code into the (small) Typst file will not only make it that much bigger, but also will make any kind of source code edits in the Typst file an absolute nightmare.


Activity
[-]Write a short and descriptive title![/-][+]Read source code (raw) from external files (like minted package in LaTeX)[/+]Gewi413 commentedon Jun 14, 2023
Are you looking for https://typst.app/docs/reference/data-loading/read/ ?
For automatic language detection, you could then
#let codeblock(filename) = raw(read(filename), block: true, lang: filename.split(".").at(-1))To then add the title to the block, you could use https://github.com/lkoehl/typst-boxes or implement the title with boxes yourself
Andrew15-5 commentedon Jun 14, 2023
I'm pretty happy with
But I've tried it on TypeScript code and I can't find a language string to enable syntax highlighting. Where can I find a list of all defined languages? Will it be expanded?
lucifer1004 commentedon Jun 16, 2023
Simply using
tsortypescriptshould work.Andrew15-5 commentedon Jun 16, 2023
I already tried both (it should, but doesn't for some reason). Typst can output colorized
latex|texcode, but nottypescript|ts.Andrew15-5 commentedon Jun 16, 2023
Ohh, now I know what was the problem. I was using the "convenient" auto PDF compilation "on type" using
typst-lspserver (in Neovim). Now I tried using standardtypst watch file.typand suddenly the colors are all here, even in React TypeScript files! That's huge, becausepygmentsstill doesn't support React files, as they are indeed harder to parse (than just TS or HTML). I love it.Now I will have to check why
typst-lspisn't the same astypst compile|watch.Andrew15-5 commentedon Jun 16, 2023
I still wish to customize colors of syntax highlighting. I saw a somewhat similar issue (#373), but it is about a new file type instead of an existing one. In
pygmentsyou have types of token (syntax unit) and you can define what color (+ bold, italic, underlined) each token will be. I want to have a custom color palette for all languages and/or for specified language (\usemintedstyle{emacs}and/or\usemintedstyle[latex]{emacs}). And of course import them from a separate file, if needed.If there is a way to do this, then I can do all the main stuff I can do with
minted. There are still things like dedent/indent, line numbers,breaksymbolleft&breaksymbolrightetc. But these features are not as important as basic "print source code from a file" and "colorize the way I like/used to it".lucifer1004 commentedon Jun 16, 2023
Customization can be done via the theme system of syntect.
Andrew15-5 commentedon Jun 16, 2023
Please, elaborate. I don't understand.
Andrew15-5 commentedon Jun 16, 2023
I looked at the roadmap. I can see that my "sub-issue" is already included:
I'm guessing it means that there is only one theme for all languages and probably no way to change them (is what I want to do).
And I found this one: #732. Then, I'll close this issue and follow the #732 as my main problem has been resolved.