Development build 9f2b6e87
Typst文档简体中文版v0.15.1 + dev 9f2b6e87

raw元素
Question mark
自始可用Go to source

Raw text with optional syntax highlighting.

Displays the text verbatim and in a monospace font. This is typically used to embed computer code into a document.

Text given to this element will ignore markup syntax, such as *strong* or _emphasis_, and will be displayed verbatim. If you would like to display content with a monospace font while still allowing markup syntax, instead of using raw, you can explicitly set the text font to a monospace font with the text.font parameter.

Raw elements are mainly produced with their dedicated syntax by enclosing text with either one or three-plus backtick characters (`) on both sides. When using three or more backticks, text immediately after the initial backticks will be treated as a language tag used for syntax highlighting, and the raw text begins after the first whitespace.

Example

Adding `rbx` to `rcx` gives the desired result. What is ```rust fn main()``` in Rust would be ```c int main()``` in C. ```rust fn main() { println!("Hello World!"); } ``` This has ``` `backticks` ``` in it (but the spaces are trimmed). And ``` here``` the leading space is also trimmed.

You can also construct a raw element programmatically from a string (and provide the language tag via the optional lang parameter).

#raw("fn " + "main() {}", lang: "rust")

If no syntax highlighting is available by default for your specified language tag (or if you want to override the built-in definition), you may provide a custom syntax specification file to the syntaxes parameter.

Styling

By default, the raw element uses the DejaVu Sans Mono font (included with Typst), with a smaller font size of 0.8em (that is, 80% of the global font size). This is because monospace fonts tend to be visually larger than non-monospace fonts.

You can customize these properties with show-set rules:

// Switch to Cascadia Code for both // inline and block raw. #show raw: set text(font: "Cascadia Code") // Reset raw blocks to the same size as normal text, // but keep inline raw at the reduced size. #show raw.where(block: true): set text(1em / 0.8) Now using the `Cascadia Code` font for raw text. Here's some Python code. It looks larger now: ```py def python(): return 5 + 5 ```

In addition, you can customize the syntax highlighting colors by setting a custom theme through the theme parameter.

For complete customization of the appearance of a raw block, a show rule on raw.line could be helpful, such as to add line numbers.

Note that in raw text, typesetting features like hyphenation, overhang, CJK-Latin spacing, and (for raw blocks) justification will be disabled by default.

Syntax

This function has dedicated syntax that produces a raw element in both markup and code mode. You can enclose text in one or three-plus backtick characters (`) on both sides to make it raw. The number of backticks must be the same on both sides, and the enclosed text cannot contain a group of that many backticks in a row. Writing just two backticks (``) produces empty raw text.

Notable differences from Markdown include that single backticks can enclose text spanning multiple lines without removing indentation, and that the three-plus backtick syntax still interprets language tags when used inline.

Raw text enclosed in single backticks has no way to specify a language tag and is always treated as inline for use within a paragraph, i.e. the block parameter is false.

Raw syntax using three or more backticks has the following properties:

These properties of the three-plus backtick syntax allow for some use cases that may not be obvious:

Embedding strings with raw syntax

A common use-case for raw syntax is to embed data as strings with formatting by accessing the .text field on raw content to get the underlying string. This may also be paired with the bytes constructor to convert the string to bytes.

An inline YAML dictionary via `.text` #yaml(bytes( ```yaml Magic: limited-by: Mana Pokémon: limited-by: Energy Yu-Gi-Oh: limited-by: false ```.text // ^^^^ used as a string ))

Language tag changes

When using raw syntax with three or more backticks, text immediately after the initial backticks (up to the first whitespace) is treated as a language tag. However in the current version of Typst, only text that would be a valid Typst identifier is treated as the language tag. The first character not valid for an identifier will be interpreted as starting the raw text.

For example, in the current version of Typst, if a raw block starts with C++, the identifier C will be the language tag, and the raw text will start with ++. If a raw block starts with ++C, it will have no language tag and the raw text will start with ++C.

To use language tags that are not valid as identifiers in the current version of Typst, you must use the lang parameter, either by calling the constructor with a string: #raw("text", lang: "..."), or by writing a set rule: #set raw(lang: "...").

In the next version of Typst, all text up to the first whitespace or backtick will be treated as the language tag, allowing a wider character set for language tags. Tags including spaces or backticks will still need to be set manually via the lang parameter.

Typst will alert you if your raw blocks will be interpreted differently in the next Typst version by emitting a warning.

参数
Question mark

text
str
必需参数
Question mark
位置参数
Question mark

The raw text.

You can also use raw blocks creatively to create custom syntaxes for your automations.

Expand展开示例:Implementing a DSL using raw and show rules
// Parse numbers in raw blocks with the // `mydsl` tag and sum them up. #show raw.where(lang: "mydsl"): it => { let sum = 0 for part in it.text.split("+") { sum += int(part.trim()) } sum } ```mydsl 1 + 2 + 3 + 4 + 5 ```

block
bool
可用set规则
Question mark
默认值:false

Whether the raw text is displayed as a separate block.

In markup mode, using one-backtick notation makes this false. Using three-backtick notation makes it true if the enclosed content contains at least one line break.

Expand展开示例
// Display inline code in a small box // that retains the correct baseline. #show raw.where(block: false): box.with( fill: luma(240), inset: (x: 3pt, y: 0pt), outset: (y: 3pt), radius: 2pt, ) // Display block code in a larger block // with more padding. #show raw.where(block: true): block.with( fill: luma(240), inset: 10pt, radius: 4pt, ) With `rg`, you can search through your files quickly. This example searches the current directory recursively for the text `Hello World`: ```bash rg "Hello World" ```

lang
none or str
可用set规则
Question mark
默认值:none

The language to interpret the raw text as for syntax highlighting.

In HTML export, this sets the data-lang attribute of the generated html.code element.

Apart from typical language tags known from Markdown, this supports the "typ", "typc", and "typm" tags for Typst markup, Typst code, and Typst math, respectively.

ExpandAvailable syntaxes
NameTags
Typsttyp, ​typst
Typst (code)typc
Typst (math)typm
ActionScriptas
Adaadb, ​ads, ​gpr
Apache Confenvvars, ​htaccess, ​HTACCESS, ​htgroups, ​HTGROUPS, ​htpasswd, ​HTPASSWD, ​.htaccess, ​.HTACCESS, ​.htgroups, ​.HTGROUPS, ​.htpasswd, ​.HTPASSWD
AppleScriptapplescript, ​script editor
AsciiDoc (Asciidoctor)adoc, ​ad, ​asciidoc
ASPasa
Assembly (x86_64)yasm, ​nasm, ​asm, ​inc, ​mac
Authorized Keysauthorized_keys, ​pub, ​authorized_keys2
AWKawk
Batch Filebat, ​cmd
BibTeXbib
Bourne Again Shell (bash)sh, ​bash, ​zsh, ​ash, ​.bash_aliases, ​.bash_completions, ​.bash_functions, ​.bash_login, ​.bash_logout, ​.bash_profile, ​.bash_variables, ​.bashrc, ​.profile, ​.textmate_init, ​.zlogin, ​.zlogout, ​.zprofile, ​.zshenv, ​.zshrc, ​PKGBUILD, ​ebuild, ​eclass
Cc, ​h
C#cs, ​csx
C++cpp, ​cc, ​cp, ​cxx, ​c++, ​C, ​h, ​hh, ​hpp, ​hxx, ​h++, ​inl, ​ipp
Cabalcabal
CFMLcfml, ​cfm, ​cfc
Clojureclj, ​cljc, ​cljs, ​edn
CMakeCMakeLists.txt, ​cmake
CMake C Headerh.in
CMake C++ Headerhh.in, ​hpp.in, ​hxx.in, ​h++.in
CMakeCacheCMakeCache.txt
CoffeeScriptcoffee, ​Cakefile, ​coffee.erb, ​cson
Comma Separated Valuescsv, ​tsv
CpuInfocpuinfo
Crontabtab, ​crontab, ​cron.d
Crystalcr
CSScss, ​css.erb, ​css.liquid
Dd, ​di
Dartdart
Diffdiff, ​patch
DockerfileDockerfile, ​dockerfile, ​.Dockerfile
DotENV.env, ​.env.dist, ​.env.local, ​.env.sample, ​.env.example, ​.env.template, ​.env.test, ​.env.test.local, ​.env.testing, ​.env.dev, ​.env.development, ​.env.development.local, ​.env.prod, ​.env.production, ​.env.production.local, ​.env.dusk.local, ​.env.staging, ​.env.default, ​.env.defaults, ​.envrc, ​.flaskenv, ​env, ​env.example, ​env.sample, ​env.template
Elixirex, ​exs
Elmelm
Emaileml, ​msg, ​mbx, ​mboxz
Erlangerl, ​hrl, ​Emakefile, ​emakefile, ​escript
F#fs, ​fsi, ​fsx
Fishfish
Fortran (Fixed Form)f, ​F, ​f77, ​F77, ​for, ​FOR, ​fpp, ​FPP
Fortran (Modern)f90, ​F90, ​f95, ​F95, ​f03, ​F03, ​f08, ​F08
Fortran Namelistnamelist
fstabfstab, ​crypttab, ​mtab
Git Attributesattributes, ​gitattributes, ​.gitattributes
Git CommitCOMMIT_EDITMSG, ​MERGE_MSG, ​TAG_EDITMSG
Git Configgitconfig, ​.gitconfig, ​.gitmodules
Git Ignoreexclude, ​gitignore, ​.gitignore
Git Link.git
Git Loggitlog
Git Mailmap.mailmap, ​mailmap
Git Rebase Todogit-rebase-todo
GLSLvs, ​fs, ​gs, ​vsh, ​fsh, ​gsh, ​vshader, ​fshader, ​gshader, ​vert, ​frag, ​geom, ​tesc, ​tese, ​comp, ​glsl, ​mesh, ​task, ​rgen, ​rint, ​rahit, ​rchit, ​rmiss, ​rcall
gnuplotgp, ​gpl, ​gnuplot, ​gnu, ​plot, ​plt
Gogo
GraphQLgraphql, ​graphqls, ​gql
Graphviz (DOT)dot, ​DOT, ​gv
Groff/troffgroff, ​troff, ​1, ​2, ​3, ​4, ​5, ​6, ​7, ​8, ​9
Groovygroovy, ​gvy, ​gradle, ​Jenkinsfile
groupgroup
Haskellhs
Highlight non-printablesshow-nonprintable
hostshosts
HTMLhtml, ​htm, ​shtml, ​xhtml
HTML (ASP)asp
HTML (EEx)html.eex, ​html.leex
HTML (Erlang)yaws
HTML (Jinja2)htm.j2, ​html.j2, ​xhtml.j2, ​xml.j2
HTML (Rails)rails, ​rhtml, ​erb, ​html.erb
HTML (Tcl)adp
HTML (Twig)twig, ​html.twig
HTTP Request and Responsehttp
INIini, ​INI, ​inf, ​INF, ​reg, ​REG, ​lng, ​cfg, ​CFG, ​desktop, ​url, ​URL, ​.editorconfig, ​.coveragerc, ​.pylintrc, ​.gitlint, ​.hgrc, ​hgrc
Javajava, ​bsh
Java Propertiesproperties
Java Server Page (JSP)jsp
JavaScriptjs, ​htc
JavaScript (Rails)js.erb
Jinja2j2, ​jinja2, ​jinja
JQjq
JSONjson, ​sublime-settings, ​sublime-menu, ​sublime-keymap, ​sublime-mousemap, ​sublime-theme, ​sublime-build, ​sublime-project, ​sublime-completions, ​sublime-commands, ​sublime-macro, ​sublime-color-scheme, ​ipynb, ​Pipfile.lock
JSON (Terraform)tfstate
jsonnetjsonnet, ​libsonnet, ​libjsonnet
Juliajl
Known Hostsknown_hosts, ​known_hosts.old
Kotlinkt, ​kts
LaTeXtex, ​ltx
Leanlean
Lessless, ​css.less
Lisplisp, ​cl, ​clisp, ​l, ​mud, ​el, ​scm, ​ss, ​lsp, ​fasl, ​sld
Literate Haskelllhs
LLVMll
loglog
Lualua
Makefilemake, ​GNUmakefile, ​makefile, ​Makefile, ​makefile.am, ​Makefile.am, ​makefile.in, ​Makefile.in, ​OCamlMakefile, ​mak, ​mk
Manpageman
Markdownmd, ​mdown, ​markdown, ​markdn
MATLABmatlab
MediaWikimediawiki, ​wikipedia, ​wiki
MemInfomeminfo
NAnt Build Filebuild
nginxconf.erb, ​conf, ​nginx.conf, ​mime.types, ​fastcgi_params, ​scgi_params, ​uwsgi_params
Nimnim, ​nims, ​nimble
Ninjaninja
Nixnix
NSISnsi, ​nsh, ​bnsi, ​bnsh, ​nsdinc
Objective-Cm, ​h
Objective-C++mm, ​M, ​h
OCamlml, ​mli
OCamllexmll
OCamlyaccmly
orgmodeorg
Pascalpas, ​p, ​dpr
passwdpasswd
Perlpl, ​pc, ​pm, ​pmc, ​pod, ​t
PHPphp, ​php3, ​php4, ​php5, ​php7, ​phps, ​phpt, ​phtml
Plain Texttxt
Protocol Bufferproto, ​protobuf, ​protodevel
Protocol Buffer (TEXT)pb.txt, ​proto.text, ​textpb, ​pbtxt, ​prototxt, ​textproto
Puppetpp, ​epp
PureScriptpurs
Pythonpy, ​py3, ​pyw, ​pyi, ​pyx, ​pyx.in, ​pxd, ​pxd.in, ​pxi, ​pxi.in, ​rpy, ​cpy, ​SConstruct, ​Sconstruct, ​sconstruct, ​SConscript, ​gyp, ​gypi, ​Snakefile, ​vpy, ​wscript, ​bazel, ​bzl
QMLqml, ​qmlproject
RR, ​r, ​Rprofile
Racketrkt
Rd (R Documentation)rd
Regorego
Regular Expressionre
Requirements.txtrequirements.txt, ​requirements.in, ​pip
resolvresolv.conf
reStructuredTextrst, ​rest
Robot Frameworkrobot, ​resource
Rubyrb, ​Appfile, ​Appraisals, ​Berksfile, ​Brewfile, ​capfile, ​cgi, ​Cheffile, ​config.ru, ​Deliverfile, ​Fastfile, ​fcgi, ​Gemfile, ​gemspec, ​Guardfile, ​irbrc, ​jbuilder, ​Podfile, ​podspec, ​prawn, ​rabl, ​rake, ​Rakefile, ​Rantfile, ​rbx, ​rjs, ​ruby.rail, ​Scanfile, ​simplecov, ​Snapfile, ​thor, ​Thorfile, ​Vagrantfile
Ruby Hamlhaml, ​sass
Ruby on Railsrxml, ​builder
Ruby Slimslim, ​skim
Rustrs
Scalascala, ​sbt, ​sc
SCSSscss
SMLsml, ​cm, ​sig
Soliditysol
SQLsql, ​ddl, ​dml
SQL (Rails)erbsql, ​sql.erb
SSH Configssh_config
SSHD Configsshd_config
Stracestrace
Stylusstyl, ​stylus
Sveltesvlt, ​svelte
Swiftswift
syslogsyslog
SystemVerilogsv, ​v, ​svh, ​vh
Tcltcl
Terraformtf, ​tfvars, ​hcl
TeXsty, ​cls
Textiletextile
Todo.txttodo.txt, ​done.txt
TOMLtoml, ​tml, ​Cargo.lock, ​Gopkg.lock, ​Pipfile, ​pdm.lock, ​poetry.lock, ​uv.lock
TypeScriptts, ​mts, ​cts
TypeScriptReacttsx
varlinkvarlink
Verilogv, ​V
VimLvim, ​vimrc, ​gvimrc, ​.vimrc, ​.gvimrc, ​_vimrc, ​_gvimrc
Vue Componentvue
Vypervy
WGSLwgsl
XMLxml, ​xsd, ​xslt, ​tld, ​dtml, ​rng, ​rss, ​opml, ​svg, ​xaml
YAMLyaml, ​yml, ​sublime-syntax
Zigzig
Expand展开示例
```typ This is *Typst!* ``` This is ```typ also *Typst*```, but inline!

align
alignment
可用set规则
Question mark
默认值:start

The horizontal alignment that each line in a raw block should have. This option is ignored if this is not a raw block (if specified block: false or single backticks were used in markup mode).

By default, this is set to start, meaning that raw text is aligned towards the start of the text direction inside the block by default, regardless of the current context’s alignment (allowing you to center the raw block itself without centering the text inside it, for example).

Expand展开示例
#set raw(align: center) ```typc let f(x) = x code = "centered" ```

syntaxes
str or path or bytes or array
可用set规则
Question mark
默认值:()

Additional syntax definitions to load. The syntax definitions should be in the sublime-syntax file format.

You can pass any of the following values:

For a list of built-in syntaxes, see the documentation of the lang parameter.

Expand展开示例
#set raw(syntaxes: "SExpressions.sublime-syntax") ```sexp (defun factorial (x) (if (zerop x) ; with a comment 1 (* x (factorial (- x 1))))) ```

theme
none or auto or str or path or bytes
可用set规则
Question mark
默认值:auto

The theme to use for syntax highlighting. Themes should be in the tmTheme file format.

You can pass any of the following values:

Applying a theme only affects the color of specifically highlighted text. It does not consider the theme’s foreground and background properties, so that you retain control over the color of raw text. You can apply the foreground color yourself with the text function and the background with a filled block. You could also use the xml function to extract these properties from the theme.

Expand展开示例
#set raw(theme: "halcyon.tmTheme") #show raw: it => block( fill: rgb("#1d2433"), inset: 8pt, radius: 5pt, text(fill: rgb("#a2aabc"), it) ) ```typ = Chapter 1 #let hi = "Hello World" ```

tab-size
int
可用set规则
Question mark
默认值:2

The size for a tab stop in spaces. A tab is replaced with enough spaces to align with the next multiple of the size.

Expand展开示例
#set raw(tab-size: 8) ```tsv Year Month Day 2000 2 3 2001 2 1 2002 3 10 ```

函数
Question mark

line元素
Question mark
0.9.0起可用Go to source

A highlighted line of raw text.

This is a helper element that is synthesized by raw elements.

It allows you to access various properties of the line, such as the line number, the raw non-highlighted text, the highlighted text, and whether it is the first or last line of the raw block.

raw.line() → content

number
int
必需参数
Question mark
位置参数
Question mark

The line number of the raw line inside of the raw block, starts at 1.

count
int
必需参数
Question mark
位置参数
Question mark

The total number of lines in the raw block.

text
str
必需参数
Question mark
位置参数
Question mark

The line of raw text.

body
content
必需参数
Question mark
位置参数
Question mark

The highlighted raw text.