Code Monkey home page Code Monkey logo

itikz's Introduction

itikz

image

image

image

Cell magic for PGF/TikZ-to-SVG rendering in Jupyter

  • Free software: MIT license

Basic Usage

Install it:

pip install itikz

Load it:

%load_ext itikz

Use it:

%%itikz --file-prefix implicit-demo- --implicit-pic
\draw[help lines] grid (5, 5);
\draw[fill=magenta!10] (1, 1) rectangle (2, 2);
\draw[fill=magenta!10] (2, 1) rectangle (3, 2);
\draw[fill=magenta!10] (3, 1) rectangle (4, 2);
\draw[fill=magenta!10] (3, 2) rectangle (4, 3);
\draw[fill=magenta!10] (2, 3) rectangle (3, 4);

Getting Started Guide

Getting Started Notebook

itikz's People

Contributors

asteppke avatar jbn avatar naereen avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

itikz's Issues

Define and reuse pgfplotset in multiple cells

  • itikz version: 0.1.5
  • Python version: 3.7
  • Operating System: Windows /WSL

Description

I would like to know it there is a way to store globally some style for pgfplot e.g.

\pgfplotsset{
	TinyAxisLabels/.style={
		yticklabel style={font=\tiny,xshift=0.5ex},
		xticklabel style={font=\tiny,yshift=0.5ex},	
		}
}

such that it can be reused in all the cells.

How to allow export to PDF?

Hello there,

Description

First of all, I LOVE your extension, I really like it and plan to use it intensely for my teaching material from now on.
The support for HTML export (including in a Jupyter book, for instance here) is excellent.
Capture d’écran_2021-02-14_18-02-49

What I Did

But I would also like to be able to export the Tikz figures to PDF, but apparently it breaks the jupyter-nbconvert --to pdf command:

$ jupyter-nbconvert --to pdf ExamplePython.ipynb
[NbConvertApp] Converting notebook Exemple de notebook avec OCaml.ipynb to pdf
Warning: Option --without-gui= is deprecated
Warning: Option --export-pdf= is deprecated

** (org.inkscape.Inkscape:6978): WARNING **: 17:31:49.318: Can't open file: /tmp/tmphabmy2re/figure.svg (doesn't exist)

** (org.inkscape.Inkscape:6978): WARNING **: 17:31:49.318: Can't open file: /tmp/tmphabmy2re/figure.svg (doesn't exist)
ink_file_open: '/tmp/tmphabmy2re/figure.svg' cannot be opened!
InkscapeApplication::document_open: Failed to open: /tmp/tmphabmy2re/figure.svg
ConcreteInkscapeApplication::on_open: failed to create document!
Traceback (most recent call last):
  File "/usr/local/bin/jupyter-nbconvert", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.6/dist-packages/jupyter_core/application.py", line 270, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/jupyter_core/application.py", line 270, in launch_instance
    return super(JupyterApp, cls).launch_instance(argv=argv, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/traitlets/config/application.py", line 664, in launch_instance
    app.start()
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/nbconvertapp.py", line 340, in start
    self.convert_notebooks()
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/nbconvertapp.py", line 510, in convert_notebooks
    self.convert_single_notebook(notebook_filename)
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/nbconvertapp.py", line 481, in convert_single_notebook
    output, resources = self.export_single_notebook(notebook_filename, resources, input_buffer=input_buffer)
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/nbconvertapp.py", line 410, in export_single_notebook
    output, resources = self.exporter.from_filename(notebook_filename, resources=resources)
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/exporters/exporter.py", line 179, in from_filename
    return self.from_file(f, resources=resources, **kw)
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/exporters/exporter.py", line 197, in from_file
    return self.from_notebook_node(nbformat.read(file_stream, as_version=4), resources=resources, **kw)
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/exporters/pdf.py", line 171, in from_notebook_node
    nb, resources=resources, **kw
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/exporters/latex.py", line 88, in from_notebook_node
    return super(LatexExporter, self).from_notebook_node(nb, resources, **kw)
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/exporters/templateexporter.py", line 307, in from_notebook_node
    nb_copy, resources = super(TemplateExporter, self).from_notebook_node(nb, resources, **kw)
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/exporters/exporter.py", line 139, in from_notebook_node
    nb_copy, resources = self._preprocess(nb_copy, resources)
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/exporters/exporter.py", line 316, in _preprocess
    nbc, resc = preprocessor(nbc, resc)
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/preprocessors/base.py", line 47, in __call__
    return self.preprocess(nb, resources)
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/preprocessors/base.py", line 69, in preprocess
    nb.cells[index], resources = self.preprocess_cell(cell, resources, index)
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/preprocessors/convertfigures.py", line 46, in preprocess_cell
    self.from_format, output.data[self.from_format])
  File "/usr/local/lib/python3.6/dist-packages/nbconvert/preprocessors/svg2pdf.py", line 110, in convert_figure
    raise TypeError("Inkscape svg to pdf conversion failed")
TypeError: Inkscape svg to pdf conversion failed

  • itikz version: 0.1.5
  • Python version: 3.6.9
  • Operating System: Ubuntu (GNU/Linux) 18.04

Thanks in advance! Regards, -- @Naereen

Placing .tex and .svg files in a separate directory

Description

Thank you for the great work!

I wanted to use --file-prefix tikz/ so that all .tex and .svg files were located in a subdirectory and did not clutter the workspace. Although it works for the .tex files, which are placed in the right place, they never get compiled because of os.path.base here.

itikz/itikz/__init__.py

Lines 129 to 131 in e5dc8e0

tex_path = output_path + ".tex"
pdf_path = output_path + ".pdf"
tex_filename = os.path.basename(tex_path)

What I want to do

I guess I am asking permission to go through the logic and make a PR enabling this use eventually.

Windows 10 Support

Thanks for this great extension! I read that you weren't sure if it worked on Windows. I tried it on my PC and all you need for Windows 10 support is to to add pdf2svg to the user's path. I guess it's obvious, but you might want to mention this in the docs.

I suggest that you have the program explicitly check for pdf2svg, and pass a simple error message if it can't be found. The existing python generated error message is quite cryptic (it's a file not found error message, which is generated since the svg file doesn't exist, since pdf2svg couldn't be found).

itikz has problems with MS-DOS paths

  • itikz version: 0.1.4
  • Python version: 3.8.5
  • Operating System: Windows 10 19041.508

Description

Render a simple tikz image im Jupyter Notebook

What I Did

Jupyter Notebook with
Cell1:
%load_ext itikz
Cell2:

%%itikz --as-jinja --temp-dir
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\end{tikzpicture}
\end{document}

Output:

This is pdfTeX, Version 3.14159265-2.6-1.40.21 (MiKTeX 20.7)
entering extended mode
! I can't find file `C:/Users/PBUERG'.
<to be read again> 
                   \protect 
<*> C:/Users/PBUERG~
                    1/AppData/Local/Temp/itikz/ae465bf47af55f5b8c746fef68307...
Please type another input file name: 
! Emergency stop.
<to be read again> 
                   \protect 
<*> C:/Users/PBUERG~
                    1/AppData/Local/Temp/itikz/ae465bf47af55f5b8c746fef68307...

!  ==> Fatal error occurred, no output PDF file produced!
Transcript written on texput.log.

As a quick fix I changed the following line in fetch_or_compile_svg
check_output([tex_program, tex_path], cwd=working_dir)
to
check_output([tex_program, os.path.realpath(tex_path)], cwd=working_dir)

This is enough to fix the problem for me, I'm not sure if other paths should also be transformed accordingly.

pdf2svg is unmaintained

The current dependencies require pdf2svg. Unfortunately the maintenance of this software has stopped several years ago (latest Windows version is from 2015). The functionality can be completely replaced by dvisvgm which is also included in TeXLive and therefore reduces the number of dependencies.

The command line syntax is a bit different and the unittests would require updating. Would you be happy with a pull request in this direction?

Add --implicit-doc

E.g for:

\documentclass[border=10pt]{standalone}
\usepackage{smartdiagram}
\begin{document}
\smartdiagram[priority descriptive diagram]{
  Develop a document structure,
  Choose a document class,
  Select suitable packages,
  Setup the document preamble,
  Write your document,
  Finetune the layout}
\end{document}

Problem multiple itikz images in a single notebook

  • itikz version: 0.1.3
  • Python version: 3.7.1
  • Operating System: Ubuntu 18.04

Description

I am experiencing problems when a notebook contains more than one itikz drawing. Somehow the text labels in the second drawing is corrupted. In the below example, if you enter the code into three separate cells, in the second plot instead of a and y the text labels show x and z.

What I Did

%load_ext itikz

%%itikz --implicit-pic --file-prefix aa- --tikz-libraries=positioning,shapes,shadows,arrows
\tikzstyle{node}=[circle, draw=black, text centered, text width=0.5cm]
\node[node] (I-0) at (0,-0) {$x$};
\node[node] (O-0) at (3,-0) {$z$};

%%itikz --implicit-pic --file-prefix ab- --tikz-libraries=positioning,shapes,arrows
\tikzstyle{node}=[circle, draw=black, text centered, text width=0.5cm]
\node[node] (I-0) at (0,0) {$a$};
\node[node] (G-0) at (3,-0) {$y$};

Node contents spill into following nodes

  • itikz version: 0.1.5
  • Python version: 3.8.3
  • Operating System: MS Windows 10

Description

When having multiple cells the node content of the first cell spills into node content of later cells.

What I Did

See image for example.

![node_content_spilling](https://user-images.githubusercontent.com/16900931/110123148-d3784700-7dc0-11eb-9ee4-995eb56e322a.PNG)

temp dir not working on Windows

  • itikz version: 0.1.4
  • Python version: 3.7
  • Operating System: Windows 10

Description

The usage of a temporary directory does not work in Windows. The issue is that in many systems the temporary directory is expanded from the %temp% environment variable. This often contains a tilde character (e.g. C:\Users\ALEXAN~1\AppData\Local\Temp). Unfortunately neither pdflatex nor lualatex can deal with the tilde character unless special workarounds are used (https://tex.stackexchange.com/questions/425954/tilde-in-a-image-path).

For itikz the fix seems easy, it operates in a given working directory so the full path is not required. I will open an appropriate pull request.

Using itikz inside functions to return SVG objects

  • itikz version: 0.1.5
  • Python version: 3.8.2
  • Operating System: Ubuntu 20.04

Description

I would like to call itikz to create an SVG object, without returning the TikZ-code first. In other words, I want to create a function that returns the SVG object returned by itikz. How can I do that?

What I Did

I've tried importing itikz and creating an TikzMagics object, but I do not know what arguments should I give to the constructor of TikzMagics.

import itikz
itikz.TikZMagics().itikz("--implicit-pic", "a")

The traceback is:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-62-b8977ef3b628> in <module>
----> 1 itikz.TikZMagics().itikz("--implicit-pic", "a")

<decorator-gen-156> in itikz(self, line, cell)

/usr/local/lib/python3.8/dist-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

/usr/local/lib/python3.8/dist-packages/itikz/__init__.py in itikz(self, line, cell)
    190             return
    191 
--> 192         ipython_ns = self.shell.user_ns
    193 
    194         if cell is None:

AttributeError: 'NoneType' object has no attribute 'user_ns'

Symbols gets messed up in Notebook

  • itikz version: 0.1.5
  • Python version: Python 3.8.10 (default, May 19 2021, 13:12:57) [MSC v.1916 64 bit (AMD64)]
  • Operating System: Win 10 20H2 19042.985
  • Jupyter Core: 6.4.0
  • Browser: Firefox 88.0.1 (64-Bit)

Description

In the first cell I'm trying to write D_p which works, in the second one I'm writing S_p and it writes D_p as well.
grafik

Same here, but even the numbers are messed up.
grafik

The generated SVG is correct, it seems the problem is that the browser seems to mix the content of the nodes when multiple SVGs are embedded in one notebook. I have now idea if this is possible, but it seems to be the case. Any ideas how to fix this?

What I Did

Here is the code again

%load_ext itikz

%%itikz --implicit-pic --temp-dir
\node at (0,0) {$D_p = 200 \mathrm{mm}$};
\node at (5,0) {$S_p = 200 \mathrm{mm}$};

%%itikz --implicit-pic --temp-dir
\node at (0,0) {$A_p = 500 \mathrm{mm}$};
\node at (7,0) {$B_p = 500 \mathrm{mm}$};

some code requires multiple executions of the engine

  • itikz version:0.1.5
  • Python version: 3.8.2
  • Operating System: linux

Description

latex engine needs executing more than once.

What I Did

%%itikz --temp-dir --implicit-standalone --tex-packages=nicematrix,tikz
\NiceMatrixOptions{code-for-last-row = \color{red}, code-for-first-row = \color{red}}
$\begin{pNiceArray}{cccc|c}[left-margin = 4pt, first-col, last-row,
    code-before =
    {
    \tikz \draw[red!55] (row-1-|col-1) -- (row-2-|col-1) -- (row-2-|col-3) -- (row-3-|col-3) -- (row-3-|col-6);
    }
]
                               & \color{red}{1}   & 2 & 1                  & 1 & \;  5 \\
                               & 0                    & 0 & \color{red}{2} & 1 & \; -1 \\
                               & 0                    & 0 & 0                  & 0 &     0 \\
\color{blue}{\begin{matrix} \text{basic variables} \\ \text{free variables} \end{matrix}} & \begin{matrix}\ x_1 \\ \\ \end{matrix} & \begin{matrix}\\ \ x_2=\alpha \\ \end{matrix} & \begin{matrix}\ x_3\\ \\ \end{matrix} & \begin{matrix} \\ \ x_4=\beta \\ \end{matrix} & \\
\end{pNiceArray}$

the tikz path is not shown

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.