-
Notifications
You must be signed in to change notification settings - Fork 470
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
284 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
BSD 3-Clause License | ||
|
||
Copyright (c) 2024-2025, KNnut | ||
|
||
Redistribution and use in source and binary forms, with or without | ||
modification, are permitted provided that the following conditions are met: | ||
|
||
1. Redistributions of source code must retain the above copyright notice, this | ||
list of conditions and the following disclaimer. | ||
|
||
2. Redistributions in binary form must reproduce the above copyright notice, | ||
this list of conditions and the following disclaimer in the documentation | ||
and/or other materials provided with the distribution. | ||
|
||
3. Neither the name of the copyright holder nor the names of its | ||
contributors may be used to endorse or promote products derived from | ||
this software without specific prior written permission. | ||
|
||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | ||
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE | ||
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | ||
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | ||
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER | ||
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | ||
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# Neoplot | ||
|
||
A Typst package to use [gnuplot](http://www.gnuplot.info/) in Typst. | ||
|
||
## Installation | ||
|
||
```typ | ||
#import "@preview/neoplot:0.0.3" as gp | ||
``` | ||
|
||
## Getting Started | ||
|
||
Execute a gnuplot script: | ||
````typ | ||
#gp.exec( | ||
```gnuplot | ||
# Remember to `reset` | ||
reset | ||
# Can add comments since it is a script | ||
set samples 1000 | ||
# Use a backslash to extend commands | ||
plot sin(x), \ | ||
cos(x) | ||
``` | ||
) | ||
```` | ||
|
||
Read a data file: | ||
``` | ||
# datafile.dat | ||
# x y | ||
0 0 | ||
2 4 | ||
4 0 | ||
``` | ||
|
||
````typ | ||
#gp.exec( | ||
```gnuplot | ||
reset | ||
$data <<EOD | ||
0 0 | ||
2 4 | ||
4 0 | ||
EOD | ||
plot $data with lines | ||
``` | ||
) | ||
```` | ||
|
||
or | ||
```typ | ||
#gp.exec( | ||
// Use a datablock since Typst doesn't support WASI | ||
"reset; $data <<EOD\n" + | ||
// Load "datafile.dat" using Typst | ||
read("datafile.dat") + | ||
"EOD\n" + | ||
"plot $data with lines" | ||
) | ||
``` | ||
|
||
## Examples | ||
|
||
A simple example: | ||
````typ | ||
// Import neoplot | ||
#import "@preview/neoplot:0.0.3" as gp | ||
#figure( | ||
gp.exec( | ||
// Set the width of the graph | ||
width: 55%, | ||
```gnuplot | ||
reset | ||
set term svg size 500,400 | ||
set xrange[-2.5*pi:2.5*pi] | ||
set yrange[-1.3:1.3] | ||
plot sin(x), cos(x) | ||
``` | ||
), | ||
caption: "Graphs of Sine and Cosine", | ||
) | ||
```` | ||
|
||
A complex use case: | ||
````typ | ||
// Import neoplot | ||
#import "@preview/neoplot:0.0.3" as gp | ||
// A csv text in Typst | ||
#let csvdata = ``` | ||
Date,A,B,C | ||
2025-01-01,1,2,3 | ||
2025-01-02,2,3,4 | ||
2025-01-03,1,2,6 | ||
2025-01-04,2,1,8 | ||
```.text | ||
#gp.exec( | ||
```gnuplot | ||
reset | ||
# Set the terminal font | ||
set term svg font "New Computer Modern,20" | ||
# Read data from an external variable | ||
$data <<EOD | ||
```.text + csvdata + | ||
```gnuplot | ||
EOD | ||
# Set the data format | ||
set datafile sep ',' columnheaders | ||
set xdata time | ||
set timefmt "%Y-%m-%d" | ||
# Set tick labels | ||
set xtics timedate format '%m-%d' time 1 day rotate by 90 right | ||
set ytics format '%.1f' | ||
# Add a legend | ||
set bmargin 4.5 | ||
set key right bmargin autotitle columnheader samplen 2 spacing 1 font ",8" | ||
# Set axis labels | ||
set xlabel "{/:Italic x}" offset 0,1 | ||
set ylabel "{/:Italic y}" offset 1,0 | ||
# Add grid lines | ||
set grid | ||
# Plot | ||
plot for [i=2:*] $data using 1:i with linespoints | ||
```.text | ||
) | ||
```` | ||
|
||
## Known Issues | ||
|
||
- Cannot output `fit.log` | ||
- No handling of `fit` exceptions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#import "neoplot.typ": exec |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
#let gp = plugin("neoplot.wasm") | ||
#let gp-exec = plugin.transition(gp.init).exec | ||
|
||
#let get-text(it) = { | ||
if type(it) == str { | ||
it | ||
} else if type(it) == content { | ||
if it.has("text") { | ||
it.text | ||
} else { | ||
panic("Content must contain field `text`") | ||
} | ||
} else { | ||
panic("Invalid type `" + type(it) + "`") | ||
} | ||
} | ||
|
||
#let get-svg-image(..args) = image(format: "svg", ..args) | ||
|
||
#let bridge(code, kind) = { | ||
let arg = cbor.encode( | ||
( | ||
code: code, | ||
type: kind, | ||
) | ||
) | ||
let output = gp-exec(arg) | ||
cbor(output) | ||
} | ||
|
||
#let image-format = ("image", "string", "bytes") | ||
|
||
#let exec( | ||
it, | ||
kind: "script", | ||
format: auto, | ||
..args, | ||
) = { | ||
if kind not in ("script", "command") { | ||
panic("Invalid code type `" + kind + "`") | ||
} | ||
if format == none { | ||
return | ||
} | ||
let code = get-text(it) | ||
if code.len() == 0 { | ||
return | ||
} | ||
if format == auto { | ||
return { | ||
let output = bridge(code, kind) | ||
if output.images != none { | ||
get-svg-image(output.images.last(), ..args) | ||
} else if output.print != none { | ||
str(output.print) | ||
} | ||
} | ||
} | ||
|
||
format = (format,).flatten().map(fmt => { | ||
if type(fmt) == type { | ||
if fmt == image { | ||
"image" | ||
} else if fmt == str { | ||
"string" | ||
} else if fmt == bytes { | ||
"bytes" | ||
} else { | ||
fmt | ||
} | ||
} else { | ||
fmt | ||
} | ||
}).dedup() | ||
for fmt in format { | ||
if fmt not in ("print", ..image-format) { | ||
panic("Invalid format `" + fmt + "`") | ||
} | ||
} | ||
|
||
let result = (:) | ||
for fmt in format { | ||
let output = bridge(code, kind) | ||
result.insert(fmt, | ||
if fmt in image-format and output.images == none { | ||
panic("No image output") | ||
} else if fmt == "image" { | ||
output.images.map(data => get-svg-image(data, ..args)) | ||
} else if fmt == "string" { | ||
output.images.map(str) | ||
} else if fmt == "bytes" { | ||
output.images | ||
} else if fmt == "print" { | ||
if output.print == none { | ||
panic("No print output") | ||
} | ||
str(output.print) | ||
} | ||
) | ||
} | ||
|
||
if result.len() == 1 { | ||
result.values().first() | ||
} else { | ||
result | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "neoplot" | ||
version = "0.0.3" | ||
entrypoint = "lib.typ" | ||
authors = ["KNnut <@KNnut>"] | ||
license = "BSD-3-Clause" | ||
description = "Gnuplot in Typst" | ||
repository = "https://github.com/KNnut/neoplot" | ||
keywords = ["gnuplot", "plotting"] | ||
categories = ["visualization", "integration"] | ||
disciplines = ["mathematics"] | ||
compiler = "0.13.0" |