├── .gitignore ├── LICENSE ├── README.md ├── demo.typ ├── lib.typ └── typst.toml /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | demo.pdf 3 | lib.pdf 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 梦飞翔 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Indenta 2 | 3 | An attempt to fix the indentation of the first paragraph in typst. 4 | 5 | It works. 6 | 7 | ## Usage 8 | 9 | ```typst 10 | #set par(first-line-indent: 2em) 11 | #import "@preview/indenta:0.0.3": fix-indent 12 | #show: fix-indent() 13 | 14 | ``` 15 | 16 | ## Demo 17 | 18 | ![image](https://github.com/flaribbit/indenta/assets/24885181/874df696-3277-4103-9166-a24639b0c7c6) 19 | 20 | ## Note 21 | 22 | When you use `fix-indent()` with other show rules, make sure to call `fix-indent()` **after other show rules**. For example: 23 | 24 | ```typst 25 | #show heading.where(level: 1): set text(size: 20pt) 26 | #show: fix-indent() 27 | ``` 28 | 29 | If you want to process the content inside your custom block, you can call `fix-indent` inside your block. For example: 30 | 31 | ```typst 32 | #block[#set text(fill: red) 33 | #show: fix-indent() 34 | 35 | Hello 36 | 37 | #table()[table] 38 | 39 | World 40 | ] 41 | ``` 42 | 43 | This package is in a very early stage and may not work as expected in some cases. Currently, there is no easy way to check if an element is inlined or not. If you got an unexpected result, you can try `fix-indent(unsafe: true)` to disable the check. 44 | 45 | Minor fixes can be made at any time, but the package in typst universe may not be updated immediately. You can check the latest version on [GitHub](https://github.com/flaribbit/indenta) then copy and paste the code into your typst file. 46 | 47 | If it still doesn't work as expected, you can try another solution (aka fake-par solution): 48 | 49 | ```typst 50 | #let fakepar=context{box();v(-measure(block()+block()).height)} 51 | #show heading: it=>it+fakepar 52 | #show figure: it=>it+fakepar 53 | #show math.equation.where(block: true): it=>it+fakepar 54 | // ... other elements 55 | ``` 56 | -------------------------------------------------------------------------------- /demo.typ: -------------------------------------------------------------------------------- 1 | #import "lib.typ": fix-indent 2 | #set par(first-line-indent: 2em) 3 | #show: fix-indent() 4 | 5 | Indent 6 | 7 | = Title 1 8 | 9 | == Section 1 10 | Indent 11 | 12 | Indent 13 | 14 | == Section 2 15 | 16 | #figure(rect(),caption: lorem(2)) 17 | no indent 18 | 19 | #figure(rect(),caption: lorem(2)) 20 | 21 | $"Indent"$ 22 | 23 | + item 24 | + item 25 | 26 | Indent 27 | 28 | = Title 2 29 | $ f(x) $ 30 | $ f(x) $ 31 | no indent 32 | 33 | Indent 34 | $ f(x) $ 35 | 36 | $ f(x) $ 37 | 38 | Indent 39 | 40 | #table()[table] 41 | 42 | Indent 43 | 44 | ```py 45 | print("hello") 46 | ``` 47 | 48 | #raw("Indent") // https://github.com/flaribbit/indenta/issues/6 49 | 50 | `Indent` 51 | 52 | = Title 3 53 | *Indent* // https://github.com/flaribbit/indenta/issues/9 54 | 55 | #quote(block: true, attribution: [Test])[An apple a day keeps the doctor away.] 56 | 57 | Indent 58 | 59 | = Title 4 60 | @fig shows a function. 61 | -------------------------------------------------------------------------------- /lib.typ: -------------------------------------------------------------------------------- 1 | // https://github.com/flaribbit/indenta 2 | #let fix-indent(unsafe: false)={ 3 | return it=>{ 4 | let _is_block(e,fn)=fn==heading or (fn==math.equation and e.block) or (fn==raw and e.has("block") and e.block) or fn==figure or fn==block or fn==list.item or fn==enum.item or fn==table or fn==grid or fn==align or (fn==quote and e.has("block") and e.block) 5 | // TODO: smallcaps returns styled(...) 6 | let _is_inline(e,fn)=fn==text or fn==box or (fn==math.equation and not e.block) or (fn==raw and not (e.has("block") and e.block)) or fn==highlight or fn==overline or fn==smartquote or fn==strike or fn==sub or fn==super or fn==underline or fn==emph or fn==strong or fn==ref or (fn==quote and not (e.has("block") and e.block)) 7 | let st=2 8 | for e in it.children{ 9 | let fn=e.func() 10 | if fn==heading{ 11 | st=2 12 | }else if _is_block(e,fn){ 13 | st=1 14 | }else if st==1{ 15 | if e==parbreak(){st=2} 16 | else if e!=[ ]{st=0} 17 | }else if st==2 and not (_is_block(e,fn) or e==[ ] or e==parbreak()){ 18 | if unsafe or _is_inline(e,fn){context h(par.first-line-indent)} 19 | st=0 20 | } 21 | e 22 | } 23 | }} 24 | -------------------------------------------------------------------------------- /typst.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "indenta" 3 | version = "0.0.3" 4 | entrypoint = "lib.typ" 5 | authors = ["梦飞翔 <@flaribbit>"] 6 | license = "MIT" 7 | description = "Fix indent of first paragraph." 8 | categories = ["utility"] 9 | keywords = ["helper", "tool"] 10 | repository = "https://github.com/flaribbit/indenta" 11 | --------------------------------------------------------------------------------