Skip to content

Read source code (raw) from external files (like minted package in LaTeX) #1494

Closed

Description

@Andrew15-5

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:

image

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:

image

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

  1. added
    feature request
    on Jun 14, 2023
  2. changed the title [-]Write a short and descriptive title![/-] [+]Read source code (raw) from external files (like minted package in LaTeX)[/+] on Jun 14, 2023
  3. Gewi413 commented on Jun 14, 2023

    @Gewi413

    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

  4. Andrew15-5 commented on Jun 14, 2023

    @Andrew15-5
    ContributorAuthor

    I'm pretty happy with

    #let frame(title: none, body) = {
      let stroke = black + 1pt
      let radius = 5pt
      let font = (font: "Fira Code", size: 10pt)
      set text(..font)
      show raw: set text(..font)
      box(stroke: stroke, radius: radius)[
        #if title != none {
            block(
              stroke: stroke,
              inset: 0.5em,
              below: 0em,
              radius: (top-left: radius, bottom-right: radius),
              title,
            )
        }
        #block(
          width: 100%,
          inset: (rest: 0.5em),
          body,
        )
      ]
    }
    
    #let include_code_file(file_path, lang) = {
      frame(title: file_path)[
        #raw(read(file_path), lang: lang)
      ]
    }

    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?

  5. lucifer1004 commented on Jun 16, 2023

    @lucifer1004
    Contributor

    Simply using ts or typescript should work.

  6. Andrew15-5 commented on Jun 16, 2023

    @Andrew15-5
    ContributorAuthor

    I already tried both (it should, but doesn't for some reason). Typst can output colorized latex|tex code, but not typescript|ts.

  7. Andrew15-5 commented on Jun 16, 2023

    @Andrew15-5
    ContributorAuthor

    Ohh, now I know what was the problem. I was using the "convenient" auto PDF compilation "on type" using typst-lsp server (in Neovim). Now I tried using standard typst watch file.typ and suddenly the colors are all here, even in React TypeScript files! That's huge, because pygments still 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-lsp isn't the same as typst compile|watch.

  8. Andrew15-5 commented on Jun 16, 2023

    @Andrew15-5
    ContributorAuthor

    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 pygments you 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 & breaksymbolright etc. But these features are not as important as basic "print source code from a file" and "colorize the way I like/used to it".

  9. lucifer1004 commented on Jun 16, 2023

    @lucifer1004
    Contributor

    Customization can be done via the theme system of syntect.

  10. Andrew15-5 commented on Jun 16, 2023

    @Andrew15-5
    ContributorAuthor

    Please, elaborate. I don't understand.

  11. Andrew15-5 commented on Jun 16, 2023

    @Andrew15-5
    ContributorAuthor

    I looked at the roadmap. I can see that my "sub-issue" is already included:

    Library

    • Customization
      • Themes for raw text and more/custom syntaxes

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions