Skip to content

Commit

Permalink
Removed quotation marks when using arg "$var"
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaslrodri committed Aug 29, 2024
1 parent 8b1e8eb commit f36ceb6
Show file tree
Hide file tree
Showing 10 changed files with 187 additions and 63 deletions.
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ All additional options are listed below:
| `-as=<str>`<br>`--input-type=<str>` | Type of the input. Possible values are: `full-document`, `standalone-document` and `tikzpicture`.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Example:* `-as=full-document`.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Defaults* to `-as=standalone-document`. |
| `-i`<br>`--implicit-pic` | Alias for `-as=tikzpicture`. |
| `-f`<br>`--full-document` | Alias for `-as=full-document`. |
| `-p=<str>`<br>`--latex-preamble=<str>` | LaTeX preamble to insert before the document.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Example:* `-p="$preamble"`, with the preamble being an IPython variable.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Defaults* to None. |
| `-p=<str>`<br>`--latex-preamble=<str>` | LaTeX preamble to insert before the document.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Example:* `-p "$preamble"`, with the preamble being an IPython variable.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Defaults* to None. |
| `-t=<str>`<br>`--tex-packages=<str>` | Comma-separated list of TeX packages.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Example:* `-t=amsfonts,amsmath`.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Defaults* to None. |
| `-nt`<br>`--no-tikz` | Force to not import the TikZ package. |
| `-l=<str>`<br>`--tikz-libraries=<str>` | Comma-separated list of TikZ libraries.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Example:* `-l=calc,arrows`.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Defaults* to None. |
Expand All @@ -220,7 +220,7 @@ All additional options are listed below:
| `-e`<br>`--full-err` | Print the full error message when an error occurs. |
| `-k`<br>`--keep-temp` | Keep temporary files. |
| `-tp=<str>`<br>`--tex-program=<str>` | TeX program to use for compilation.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Example:* `-tp=xelatex` or `-tp=lualatex`.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Defaults* to `-tp=pdflatex`. |
| `-ta=<str>`<br>`--tex-args=<str>` | Arguments to pass to the TeX program.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Example:* `-ta="$tex_args_ipython_variable"`.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Defaults* to None. |
| `-ta=<str>`<br>`--tex-args=<str>` | Arguments to pass to the TeX program.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Example:* `-ta "$tex_args_ipython_variable"`.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Defaults* to None. |
| `-nc`<br>`--no-compile` | Do not compile the TeX code. |
| `-s=<str>`<br>`--save-tikz=<str>` | Save the TikZ code to file.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Example:* `-s filename.tikz`.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Defaults* to None. |
| `-st=<str>`<br>`--save-tex=<str>` | Save full LaTeX code to file.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Example:* `-st filename.tex`.<br>&nbsp;&nbsp;&nbsp;&nbsp;*Defaults* to None. |
Expand All @@ -241,6 +241,12 @@ Contributions are welcome from everyone! Whether you're reporting bugs, submitti

All notable changes to this project are presented below.

## v0.5.5

**🐞 Bug Fixes**

- Removed quotation marks when using `arg "$var"` (e.g., `-p "$preamble"`).

## v0.5.4

**🐞 Bug Fixes**
Expand Down
6 changes: 6 additions & 0 deletions docs/about/changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
All notable changes to this project are presented below.

## v0.5.5

**🐞 Bug Fixes**

- Removed quotation marks when using `arg "$var"` (e.g., `-p "$preamble"`).

## v0.5.4

**🐞 Bug Fixes**
Expand Down
11 changes: 7 additions & 4 deletions docs/usage/as-magic.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ options:
-f, --full-document Alias for `-as=full-document`.
-p LATEX_PREAMBLE, --latex-preamble LATEX_PREAMBLE
LaTeX preamble to insert before the document, e.g.,
`-p="$preamble"`, with the preamble being an IPython
`-p "$preamble"`, with the preamble being an IPython
variable.
-t TEX_PACKAGES, --tex-packages TEX_PACKAGES
Comma-separated list of TeX packages, e.g.,
Expand Down Expand Up @@ -108,8 +108,8 @@ options:
`-tp=xelatex` or `-tp=lualatex`. Defaults to
`-tp=pdflatex`.
-ta TEX_ARGS, --tex-args TEX_ARGS
Arguments to pass to the TeX program, e.g.,
`-ta="$tex_args_ipython_variable"`.
Arguments to pass to the TeX program, e.g., `-ta
"$tex_args_ipython_variable"`.
-nc, --no-compile Do not compile the TeX code.
-s SAVE_TIKZ, --save-tikz SAVE_TIKZ
Save the TikZ code to file, e.g., `-s filename.tikz`.
Expand Down Expand Up @@ -955,14 +955,17 @@ tex_params = "--enable-write18 --extra-mem-top=10000000"
```

```latex
%%tikz -as=tikz -t=pgfplots -nt -tp=pdflatex --tex-args="$tex_params"
%%tikz -as=tikz -t=pgfplots -nt -tp=pdflatex --tex-args "$tex_params"
\begin{axis}[
xlabel=$x$,
ylabel={$f(x) = x^2 - x +4$}
]
\addplot {x^2 - x +4};
\end{axis}
```
<div class="result" markdown>
![Another quadratic formula](../assets/tikz/another_quadratic.svg)
</div>

## Logging and debugging

Expand Down
99 changes: 56 additions & 43 deletions jupyter_tikz/jupyter_tikz.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ def full_latex(self) -> str:
"type": str,
"default": None,
"desc": "LaTeX preamble to insert before the document",
"example": '`-p="$preamble"`, with the preamble being an IPython variable',
"example": '`-p "$preamble"`, with the preamble being an IPython variable',
},
"tex-packages": {
"short-arg": "t",
Expand Down Expand Up @@ -516,7 +516,7 @@ def full_latex(self) -> str:
"type": str,
"default": None,
"desc": "Arguments to pass to the TeX program",
"example": '`-ta="$tex_args_ipython_variable"`',
"example": '`-ta "$tex_args_ipython_variable"`',
},
"no-compile": {
"short-arg": "nc",
Expand Down Expand Up @@ -603,6 +603,14 @@ def decorator(magic_command):
return decorator


def _remove_wrapping_quotes(text):
# Define the regex pattern to match a string wrapped with quotation marks
pattern = re.compile(r'^"(.*)"$', re.DOTALL)

# Use re.sub to replace the matched pattern with the content inside the quotation marks
return pattern.sub(r"\1", text)


@magics_class
class TikZMagics(Magics):
def _get_input_type(self, input_type: str) -> str | None:
Expand Down Expand Up @@ -651,41 +659,46 @@ def tikz(self, line, cell: str | None = None, local_ns=None) -> Image | SVG | No
...: (m-2-1) edge node [below] {$cd$} (m-2-2);
"""

self.args = parse_argstring(self.tikz, line)
self.args: dict = vars(parse_argstring(self.tikz, line))

for key, value in self.args.items():
if not (isinstance(value, str)):
continue
self.args[key] = _remove_wrapping_quotes(value)

if self.args.latex_preamble and (
self.args.tex_packages
or self.args.tikz_libraries
or self.args.pgfplots_libraries
if self.args["latex_preamble"] and (
self.args["tex_packages"]
or self.args["tikz_libraries"]
or self.args["pgfplots_libraries"]
):
print(_EXTRAS_CONFLITS_ERR, file=sys.stderr)
return

if (self.args.implicit_pic and self.args.full_document) or (
(self.args.implicit_pic or self.args.full_document)
and self.args.input_type != "standalone-document"
if (self.args["implicit_pic"] and self.args["full_document"]) or (
(self.args["implicit_pic"] or self.args["full_document"])
and self.args["input_type"] != "standalone-document"
):
print(
_INPUT_TYPE_CONFLIT_ERR,
file=sys.stderr,
)
return
if self.args.print_jinja and self.args.print_tex:
if self.args["print_jinja"] and self.args["print_tex"]:
print(
_PRINT_CONFLICT_ERR,
file=sys.stderr,
)
return

if self.args.implicit_pic:
if self.args["implicit_pic"]:
self.input_type = "tikzpicture"
elif self.args.full_document:
elif self.args["full_document"]:
self.input_type = "full-document"
else:
self.input_type = self._get_input_type(self.args.input_type)
self.input_type = self._get_input_type(self.args["input_type"])
if self.input_type is None:
print(
f"`{self.args.input_type}` is not a valid input type.",
f'`{self.args["input_type"]}` is not a valid input type.',
"Valid input types are `full-document`, `standalone-document`, or `tikzpicture`.",
file=sys.stderr,
)
Expand All @@ -695,58 +708,58 @@ def tikz(self, line, cell: str | None = None, local_ns=None) -> Image | SVG | No
local_ns = local_ns or {}

if cell is None:
if self.args.code is None:
if self.args["code"] is None:
print('Use "%tikz?" for help', file=sys.stderr)
return

if self.args.code not in local_ns:
self.src: str = self.args.code
if self.args["code"] not in local_ns:
self.src: str = self.args["code"]
else:
self.src: str = local_ns[self.args.code]
self.src: str = local_ns[self.args["code"]]

if self.input_type == "full-document":
self.tex_obj = TexDocument(
self.src, no_jinja=self.args.no_jinja, ns=local_ns
self.src, no_jinja=self.args["no_jinja"], ns=local_ns
)
else:
implicit_tikzpicture = self.input_type == "tikzpicture"
self.tex_obj = TexFragment(
self.src,
implicit_tikzpicture=implicit_tikzpicture,
preamble=self.args.latex_preamble,
tex_packages=self.args.tex_packages,
no_tikz=self.args.no_tikz,
tikz_libraries=self.args.tikz_libraries,
pgfplots_libraries=self.args.pgfplots_libraries,
scale=self.args.scale,
no_jinja=self.args.no_jinja,
preamble=self.args["latex_preamble"],
tex_packages=self.args["tex_packages"],
no_tikz=self.args["no_tikz"],
tikz_libraries=self.args["tikz_libraries"],
pgfplots_libraries=self.args["pgfplots_libraries"],
scale=self.args["scale"],
no_jinja=self.args["no_jinja"],
ns=local_ns,
)

if self.args.print_jinja:
if self.args["print_jinja"]:
print(self.tex_obj)
if self.args.print_tex:
if self.args["print_tex"]:
print(self.tex_obj.full_latex)

image = None
if not self.args.no_compile:
if not self.args["no_compile"]:
image = self.tex_obj.run_latex(
tex_program=self.args.tex_program,
tex_args=self.args.tex_args,
rasterize=self.args.rasterize,
full_err=self.args.full_err,
keep_temp=self.args.keep_temp,
save_tikz=self.args.save_tikz,
save_tex=self.args.save_tex,
save_pdf=self.args.save_pdf,
save_image=self.args.save_image,
dpi=self.args.dpi,
grayscale=self.args.gray,
tex_program=self.args["tex_program"],
tex_args=self.args["tex_args"],
rasterize=self.args["rasterize"],
full_err=self.args["full_err"],
keep_temp=self.args["keep_temp"],
save_tikz=self.args["save_tikz"],
save_tex=self.args["save_tex"],
save_pdf=self.args["save_pdf"],
save_image=self.args["save_image"],
dpi=self.args["dpi"],
grayscale=self.args["gray"],
)
if image is None:
return None

if self.args.save_var:
local_ns[self.args.save_var] = str(self.tex_obj)
if self.args["save_var"]:
local_ns[self.args["save_var"]] = str(self.tex_obj)

return image
20 changes: 9 additions & 11 deletions notebooks/GettingStarted.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"scrolled": true
},
"metadata": {},
"outputs": [
{
"data": {
Expand Down Expand Up @@ -250,7 +248,7 @@
" -f, --full-document Alias for `-as=full-document`.\n",
" -p LATEX_PREAMBLE, --latex-preamble LATEX_PREAMBLE\n",
" LaTeX preamble to insert before the document, e.g.,\n",
" `-p=\"$preamble\"`, with the preamble being an IPython\n",
" `-p \"$preamble\"`, with the preamble being an IPython\n",
" variable.\n",
" -t TEX_PACKAGES, --tex-packages TEX_PACKAGES\n",
" Comma-separated list of TeX packages, e.g.,\n",
Expand Down Expand Up @@ -279,8 +277,8 @@
" `-tp=xelatex` or `-tp=lualatex`. Defaults to\n",
" `-tp=pdflatex`.\n",
" -ta TEX_ARGS, --tex-args TEX_ARGS\n",
" Arguments to pass to the TeX program, e.g.,\n",
" `-ta=\"$tex_args_ipython_variable\"`.\n",
" Arguments to pass to the TeX program, e.g., `-ta\n",
" \"$tex_args_ipython_variable\"`.\n",
" -nc, --no-compile Do not compile the TeX code.\n",
" -s SAVE_TIKZ, --save-tikz SAVE_TIKZ\n",
" Save the TikZ code to file, e.g., `-s filename.tikz`.\n",
Expand Down Expand Up @@ -3647,7 +3645,7 @@
}
],
"source": [
"%%tikz -as=tikz -t=pgfplots -nt -tp=pdflatex --tex-args=\"$tex_params\"\n",
"%%tikz -as=tikz -t=pgfplots -nt -tp=pdflatex --tex-args \"$tex_params\"\n",
"\\begin{axis}[\n",
" xlabel=$x$,\n",
" ylabel={$f(x) = x^2 - x +4$}\n",
Expand Down Expand Up @@ -3735,7 +3733,7 @@
"(C:/Users/lucas/Github/jupyter-tikz/notebooks/1229a42e3a5f19a871fbae6bb079e726.\n",
"tex\n",
"LaTeX2e <2024-06-01> patch level 2\n",
"L3 programming layer <2024-05-27>\n",
"L3 programming layer <2024-07-20>\n",
"\n",
"(C:\\Users\\lucas\\AppData\\Local\\Programs\\MiKTeX\\tex/latex/standalone\\standalone.c\n",
"ls\n",
Expand All @@ -3751,7 +3749,7 @@
")\n",
"(C:\\Users\\lucas\\AppData\\Local\\Programs\\MiKTeX\\tex/latex/standalone\\standalone.c\n",
"fg) (C:\\Users\\lucas\\AppData\\Local\\Programs\\MiKTeX\\tex/latex/base\\article.cls\n",
"Document Class: article 2023/05/17 v1.4n Standard LaTeX document class\n",
"Document Class: article 2024/02/08 v1.4n Standard LaTeX document class\n",
"(C:\\Users\\lucas\\AppData\\Local\\Programs\\MiKTeX\\tex/latex/base\\size10.clo)))\n",
"(C:\\Users\\lucas\\AppData\\Local\\Programs\\MiKTeX\\tex/latex/pgf/frontendlayer\\tikz.\n",
"sty\n",
Expand Down Expand Up @@ -4860,7 +4858,7 @@
}
],
"source": [
"%%tikz -as=tikzpicture --rasterize --dpi=300 -l=arrows,automata\n",
"%%tikz -as=tikzpicture --rasterize --dpi=300 -l \"arrows, automata\"\n",
"\\draw (0,0) rectangle (1,1);\n",
"\\filldraw (0.5,0.5) circle (.1);"
]
Expand Down Expand Up @@ -5026,7 +5024,7 @@
"<use xlink:href=\"#glyph-1-0\" x=\"7.89794\" y=\"65.389997\"/>\n",
"</g>\n",
"<path fill=\"none\" stroke-width=\"0.3985\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke=\"rgb(0%, 0%, 0%)\" stroke-opacity=\"1\" stroke-miterlimit=\"10\" d=\"M 0.0009521 85.042004 L 26.645053 85.042004 M 58.395153 85.042004 L 85.039254 85.042004 \" transform=\"matrix(0.992246, 0, 0, -0.992246, 26.713899, 103.968518)\"/>\n",
"<path fill=\"none\" stroke-width=\"0.797\" stroke-linecap=\"butt\" stroke-linejoin=\"bevel\" stroke=\"rgb(0%, 0%, 0%)\" stroke-opacity=\"1\" stroke-miterlimit=\"10\" d=\"M 26.247439 85.042004 L 29.325998 90.99441 L 34.68395 79.085662 L 40.041902 90.99441 L 45.399855 79.085662 L 50.757807 90.99441 L 56.115759 79.085662 L 58.792767 85.042004 \" transform=\"matrix(0.992246, 0, 0, -0.992246, 26.713899, 103.968518)\"/>\n",
"<path fill=\"none\" stroke-width=\"0.797\" stroke-linecap=\"butt\" stroke-linejoin=\"bevel\" stroke=\"rgb(0%, 0%, 0%)\" stroke-opacity=\"1\" stroke-miterlimit=\"10\" d=\"M 26.247439 85.042004 L 29.290567 90.99441 L 34.581594 79.085662 L 39.872621 90.99441 L 45.167585 79.085662 L 50.458612 90.99441 L 55.749639 79.085662 L 58.792767 85.042004 \" transform=\"matrix(0.992246, 0, 0, -0.992246, 26.713899, 103.968518)\"/>\n",
"<path fill-rule=\"nonzero\" fill=\"rgb(0%, 0%, 0%)\" fill-opacity=\"1\" stroke-width=\"0.3985\" stroke-linecap=\"butt\" stroke-linejoin=\"miter\" stroke=\"rgb(0%, 0%, 0%)\" stroke-opacity=\"1\" stroke-miterlimit=\"10\" d=\"M 86.625774 85.042004 C 86.625774 85.915969 85.917155 86.628525 85.039254 86.628525 C 84.165289 86.628525 83.452733 85.915969 83.452733 85.042004 C 83.452733 84.164103 84.165289 83.451547 85.039254 83.451547 C 85.917155 83.451547 86.625774 84.164103 86.625774 85.042004 Z M 86.625774 85.042004 \" transform=\"matrix(0.992246, 0, 0, -0.992246, 26.713899, 103.968518)\"/>\n",
"<g fill=\"rgb(0%, 0%, 0%)\" fill-opacity=\"1\">\n",
"<use xlink:href=\"#glyph-0-1\" x=\"62.933851\" y=\"9.898645\"/>\n",
Expand Down
Binary file modified notebooks/outputs/a_dot.pdf
Binary file not shown.
Binary file modified notebooks/outputs/angle.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "jupyter-tikz"
version = "0.5.4"
version = "0.5.5"
description = "IPython Magics for rendering TeX/TikZ in Jupyter Notebooks"
license = "MIT"
authors = ["lucaslrodri"]
Expand Down
Loading

0 comments on commit f36ceb6

Please sign in to comment.