Skip to content

Commit d9f636f

Browse files
authored
Merge pull request #385 from flying-sheep/context
Add ConTeXt support
2 parents 22b7a5c + 4d1c787 commit d9f636f

31 files changed

+189
-74
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
- run: sudo apt update
1818
# <https://stackoverflow.com/a/44333806/353337>
1919
- run: DEBIAN_FRONTEND=noninteractive sudo apt install tzdata
20-
- run: sudo apt install -y texlive-latex-base texlive-latex-extra
20+
- run: sudo apt install -y texlive-latex-base texlive-latex-extra context
2121
- run: pip3 install -U pytest pytest-cov excode --user
2222
- checkout
2323
- run: excode README.md test/zzz_readme_test.py --filter python,test

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
python-version: "3.x"
2929
- uses: actions/checkout@v1
3030
- name: Install dependencies
31-
run: sudo apt-get install -y texlive-latex-base texlive-latex-extra python3-tk python3-scipy
31+
run: sudo apt-get install -y texlive-latex-base texlive-latex-extra context python3-tk python3-scipy
3232
- name: Install package
3333
run: |
3434
pip install --upgrade pip

README.md

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ figures like
1818

1919
![](https://nschloe.github.io/tikzplotlib/example.png)
2020

21-
for native inclusion into LaTeX documents.
21+
for native inclusion into LaTeX or ConTeXt documents.
2222

2323
The output of tikzplotlib is in
24-
[PGFPlots](http://pgfplots.sourceforge.net/pgfplots.pdf), a LaTeX library that sits on
24+
[PGFPlots](http://pgfplots.sourceforge.net/pgfplots.pdf), a TeX library that sits on
2525
top of [PGF/TikZ](https://en.wikipedia.org/wiki/PGF/TikZ) and describes graphs in terms
2626
of axes, data etc. Consequently, the output of tikzplotlib
2727

@@ -95,7 +95,7 @@ table {%
9595
```
9696
(Use `get_tikz_code()` instead of `save()` if you want the code as a string.)
9797

98-
Tweaking the plot is straightforward and can be done as part of your LaTeX work flow.
98+
Tweaking the plot is straightforward and can be done as part of your TeX work flow.
9999
[The fantastic PGFPlots manual](http://pgfplots.sourceforge.net/pgfplots.pdf) contains
100100
great examples of how to make your plot look even better.
101101

@@ -120,10 +120,12 @@ to install/update.
120120
```python
121121
import tikzplotlib
122122
tikzplotlib.save("mytikz.tex")
123+
# or
124+
tikzplotlib.save("mytikz.tex", flavor="context")
123125
```
124126
to store the TikZ file as `mytikz.tex`.
125127

126-
3. Add the contents of `mytikz.tex` into your LaTeX source code. A convenient way of
128+
3. Add the contents of `mytikz.tex` into your TeX source code. A convenient way of
127129
doing so is via
128130
```latex
129131
\input{/path/to/mytikz.tex}
@@ -132,11 +134,29 @@ to install/update.
132134
included in the header of your document:
133135
```latex
134136
\usepackage[utf8]{inputenc}
135-
\usepackage{fontspec} % optional
136137
\usepackage{pgfplots}
138+
\DeclareUnicodeCharacter{2212}{−}
139+
\usepgfplotslibrary{groupplots,dateplot}
140+
\usetikzlibrary{patterns,shapes.arrows}
137141
\pgfplotsset{compat=newest}
138-
\usepgfplotslibrary{groupplots}
139-
\usepgfplotslibrary{dateplot}
142+
```
143+
or:
144+
```latex
145+
\setupcolors[state=start]
146+
\usemodule[tikz]
147+
\usemodule[pgfplots]
148+
\usepgfplotslibrary[groupplots,dateplot]
149+
\usetikzlibrary[patterns,shapes.arrows]
150+
\pgfplotsset{compat=newest}
151+
\unexpanded\def\startgroupplot{\groupplot}
152+
\unexpanded\def\stopgroupplot{\endgroupplot}
153+
```
154+
You can also get the code via:
155+
```python
156+
import tikzplotlib
157+
tikzplotlib.Flavors.latex.preamble()
158+
# or
159+
tikzplotlib.Flavors.context.preamble()
140160
```
141161

142162
4. Optional: clean up the figure before exporting to tikz using the `clean_figure` command.

test/helpers.py

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ def _unidiff_output(expected, actual):
3131

3232

3333
def assert_equality(
34-
plot, filename, assert_compilation=True, **extra_get_tikz_code_args
34+
plot, filename, assert_compilation=True, flavor="latex", **extra_get_tikz_code_args
3535
):
3636
plot()
3737
code = tikzplotlib.get_tikz_code(
38-
include_disclaimer=False, float_format=".8g", **extra_get_tikz_code_args
38+
include_disclaimer=False,
39+
float_format=".8g",
40+
flavor=flavor,
41+
**extra_get_tikz_code_args,
3942
)
4043
plt.close()
4144

@@ -47,13 +50,16 @@ def assert_equality(
4750
if assert_compilation:
4851
plot()
4952
code = tikzplotlib.get_tikz_code(
50-
include_disclaimer=False, standalone=True, **extra_get_tikz_code_args
53+
include_disclaimer=False,
54+
standalone=True,
55+
flavor=flavor,
56+
**extra_get_tikz_code_args,
5157
)
5258
plt.close()
53-
assert _compile(code) is not None, code
59+
assert _compile(code, flavor) is not None, code
5460

5561

56-
def _compile(code):
62+
def _compile(code, flavor):
5763
_, tmp_base = tempfile.mkstemp()
5864

5965
tex_file = tmp_base + ".tex"
@@ -64,13 +70,14 @@ def _compile(code):
6470
os.chdir(os.path.dirname(tex_file))
6571

6672
# compile the output to pdf
73+
cmdline = dict(
74+
latex=["pdflatex", "--interaction=nonstopmode"],
75+
context=["context", "--nonstopmode"],
76+
)[flavor]
6777
try:
68-
subprocess.check_output(
69-
["pdflatex", "--interaction=nonstopmode", tex_file],
70-
stderr=subprocess.STDOUT,
71-
)
78+
subprocess.check_output(cmdline + [tex_file], stderr=subprocess.STDOUT)
7279
except subprocess.CalledProcessError as e:
73-
print("pdflatex output:")
80+
print(f"{cmdline[0]} output:")
7481
print("=" * 70)
7582
print(e.output.decode("utf-8"))
7683
print("=" * 70)
@@ -81,15 +88,15 @@ def _compile(code):
8188
return output_pdf
8289

8390

84-
def compare_mpl_latex(plot):
91+
def compare_mpl_tex(plot, flavor="latex"):
8592
plot()
8693
code = tikzplotlib.get_tikz_code(standalone=True)
8794
directory = os.getcwd()
8895
filename = "test-0.png"
8996
plt.savefig(filename)
9097
plt.close()
9198

92-
pdf_file = _compile(code)
99+
pdf_file = _compile(code, flavor)
93100
pdf_dirname = os.path.dirname(pdf_file)
94101

95102
# Convert PDF to PNG.

test/test_annotate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ def test():
4444
if __name__ == "__main__":
4545
import helpers
4646

47-
helpers.compare_mpl_latex(plot)
47+
helpers.compare_mpl_tex(plot)
4848
# helpers.print_tree(plot())

test/test_arrows.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,5 @@ def to_texstring(s):
7070
if __name__ == "__main__":
7171
import helpers
7272

73-
helpers.compare_mpl_latex(plot)
73+
helpers.compare_mpl_tex(plot)
7474
# helpers.print_tree(plot())

test/test_axvline.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ def test():
2222
if __name__ == "__main__":
2323
import helpers
2424

25-
helpers.compare_mpl_latex(plot)
25+
helpers.compare_mpl_tex(plot)
2626
# helpers.print_tree(plot())

test/test_barchart_legend.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,5 +40,5 @@ def test():
4040
if __name__ == "__main__":
4141
import helpers
4242

43-
helpers.compare_mpl_latex(plot)
43+
helpers.compare_mpl_tex(plot)
4444
# helpers.print_tree(plot())

test/test_basic_sin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ def test():
2929
if __name__ == "__main__":
3030
import helpers
3131

32-
helpers.compare_mpl_latex(plot)
32+
helpers.compare_mpl_tex(plot)
3333
# helpers.print_tree(plot())

test/test_colorbars.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,5 @@ def test():
8181
if __name__ == "__main__":
8282
import helpers
8383

84-
helpers.compare_mpl_latex(plot)
84+
helpers.compare_mpl_tex(plot)
8585
# helpers.print_tree(plot())

0 commit comments

Comments
 (0)