Skip to content

Commit 326f876

Browse files
author
Kasper Peeters
committed
Fix typesetting of \verb|...| nodes.
1 parent da3cde5 commit 326f876

File tree

6 files changed

+141
-97
lines changed

6 files changed

+141
-97
lines changed

core/properties/LaTeXForm.cnb

Lines changed: 95 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,132 +1,138 @@
11
{
2-
"cell_id" : 10332960512342882681,
3-
"cells" :
4-
[
2+
"cell_id": 10332960512342882681,
3+
"cells": [
54
{
6-
"cell_id" : 16101808746726886949,
7-
"cell_origin" : "client",
8-
"cell_type" : "latex",
9-
"cells" :
10-
[
5+
"cell_id": 16101808746726886949,
6+
"cell_origin": "client",
7+
"cell_type": "latex",
8+
"cells": [
119
{
12-
"cell_id" : 629152003197411965,
13-
"cell_origin" : "client",
14-
"cell_type" : "latex_view",
15-
"source" : "\\property{LaTeXForm}{Changes the way in which expressions are displayed.}\n\nChanges the way in which expressions are displayed in the graphical\ninterface. Example:"
10+
"cell_id": 17922711406420684199,
11+
"cell_origin": "client",
12+
"cell_type": "latex_view",
13+
"source": "\\property{LaTeXForm}{Changes the way in which expressions are displayed.}\n\nThe \\prop{LaTeXForm} property changes the way in which expressions are displayed in the graphical\ninterface. In its simplest form, it acts just like a node rename operation,"
1614
}
1715
],
18-
"hidden" : true,
19-
"source" : "\\property{LaTeXForm}{Changes the way in which expressions are displayed.}\n\nChanges the way in which expressions are displayed in the graphical\ninterface. Example:"
16+
"hidden": true,
17+
"source": "\\property{LaTeXForm}{Changes the way in which expressions are displayed.}\n\nThe \\prop{LaTeXForm} property changes the way in which expressions are displayed in the graphical\ninterface. In its simplest form, it acts just like a node rename operation,"
2018
},
2119
{
22-
"cell_id" : 8166426365570956951,
23-
"cell_origin" : "client",
24-
"cell_type" : "input",
25-
"cells" :
26-
[
20+
"cell_id": 8166426365570956951,
21+
"cell_origin": "client",
22+
"cell_type": "input",
23+
"cells": [
2724
{
28-
"cell_id" : 17154926141228239702,
29-
"cell_origin" : "server",
30-
"cell_type" : "latex_view",
31-
"cells" :
32-
[
25+
"cell_id": 17154926141228239702,
26+
"cell_origin": "server",
27+
"cell_type": "latex_view",
28+
"cells": [
3329
{
34-
"cell_id" : 76011697136639576,
35-
"cell_origin" : "server",
36-
"cell_type" : "input_form",
37-
"source" : "\\del_{m}(A)"
30+
"cell_id": 76011697136639576,
31+
"cell_origin": "server",
32+
"cell_type": "input_form",
33+
"source": "\\del_{m}(A)"
3834
}
3935
],
40-
"source" : "\\begin{dmath*}{}\\partial_{m}\\left(A\\right)\\end{dmath*}"
36+
"source": "\\begin{dmath*}{}\\partial_{m}\\left(A\\right)\\end{dmath*}"
4137
}
4238
],
43-
"source" : "\\del{#}::LaTeXForm(\"\\partial\").\nex:=\\del_{m}(A);"
39+
"source": "\\del{#}::LaTeXForm(\"\\partial\").\nex:=\\del_{m}(A);"
4440
},
4541
{
46-
"cell_id" : 12137153977390704291,
47-
"cell_origin" : "client",
48-
"cell_type" : "latex",
49-
"cells" :
50-
[
42+
"cell_id": 12137153977390704291,
43+
"cell_origin": "client",
44+
"cell_type": "latex",
45+
"cells": [
5146
{
52-
"cell_id" : 1199599436471906871,
53-
"cell_origin" : "client",
54-
"cell_type" : "latex_view",
55-
"source" : "This prints $\\partial_{m}(A);$ in the notebook, despite the fact\nthat \\verb|\\del| is not a LaTeX{} command.\n\nIf you use this property to make a symbol printable, make sure to\ndeclare it \\emph{before} any other properties are declared, otherwise\nthe notebook will not know how to display the symbol and produce an\nerror message.\n\nNote that the property is attached to a pattern (\\verb|\\del{#}| in\nthis case) which matches the expression in which the replacement has\nto be made. If the pattern matches, the replacement will be done on\nthe head symbol (\\verb|\\del| in this case). A pattern \\verb|\\del|\nwithout the argument wildcard \\verb|#| would only replace\nwhen \\verb|\\del| occurs without any arguments (as in \ne.g.~\\verb|\\del + A|)."
47+
"cell_id": 1199599436471906871,
48+
"cell_origin": "client",
49+
"cell_type": "latex_view",
50+
"source": "This prints $\\partial_{m}(A);$ in the notebook, despite the fact\nthat \\verb|\\del| is not a LaTeX{} command.\n\nIf you use this property to make a symbol printable, make sure to\ndeclare it \\emph{before} any other properties are declared, otherwise\nthe notebook will not know how to display the symbol and produce an\nerror message.\n\nNote that the property is attached to a pattern (\\verb|\\del{#}| in\nthis case) which matches the expression in which the replacement has\nto be made. If the pattern matches, the replacement will be done on\nthe head symbol (\\verb|\\del| in this case). A pattern \\verb|\\del|\nwithout the argument wildcard \\verb|#| would only replace\nwhen \\verb|\\del| occurs without any arguments (as in \ne.g.~\\verb|\\del + A|)."
5651
}
5752
],
58-
"hidden" : true,
59-
"source" : "This prints $\\partial_{m}(A);$ in the notebook, despite the fact\nthat \\verb|\\del| is not a LaTeX{} command.\n\nIf you use this property to make a symbol printable, make sure to\ndeclare it \\emph{before} any other properties are declared, otherwise\nthe notebook will not know how to display the symbol and produce an\nerror message.\n\nNote that the property is attached to a pattern (\\verb|\\del{#}| in\nthis case) which matches the expression in which the replacement has\nto be made. If the pattern matches, the replacement will be done on\nthe head symbol (\\verb|\\del| in this case). A pattern \\verb|\\del|\nwithout the argument wildcard \\verb|#| would only replace\nwhen \\verb|\\del| occurs without any arguments (as in \ne.g.~\\verb|\\del + A|)."
53+
"hidden": true,
54+
"source": "This prints $\\partial_{m}(A);$ in the notebook, despite the fact\nthat \\verb|\\del| is not a LaTeX{} command.\n\nIf you use this property to make a symbol printable, make sure to\ndeclare it \\emph{before} any other properties are declared, otherwise\nthe notebook will not know how to display the symbol and produce an\nerror message.\n\nNote that the property is attached to a pattern (\\verb|\\del{#}| in\nthis case) which matches the expression in which the replacement has\nto be made. If the pattern matches, the replacement will be done on\nthe head symbol (\\verb|\\del| in this case). A pattern \\verb|\\del|\nwithout the argument wildcard \\verb|#| would only replace\nwhen \\verb|\\del| occurs without any arguments (as in \ne.g.~\\verb|\\del + A|)."
6055
},
6156
{
62-
"cell_id" : 16855295245100204448,
63-
"cell_origin" : "client",
64-
"cell_type" : "latex",
65-
"cells" :
66-
[
57+
"cell_id": 16855295245100204448,
58+
"cell_origin": "client",
59+
"cell_type": "latex",
60+
"cells": [
6761
{
68-
"cell_id" : 12511356882212983496,
69-
"cell_origin" : "client",
70-
"cell_type" : "latex_view",
71-
"source" : "If you want more control over the way in which elements of your original expression \nare printed, you can use patterns like in the example below:"
62+
"cell_id": 12511356882212983496,
63+
"cell_origin": "client",
64+
"cell_type": "latex_view",
65+
"source": "If you want more control over the way in which elements of your original expression \nare printed, you can use patterns like in the example below:"
7266
}
7367
],
74-
"hidden" : true,
75-
"source" : "If you want more control over the way in which elements of your original expression \nare printed, you can use patterns like in the example below:"
68+
"hidden": true,
69+
"source": "If you want more control over the way in which elements of your original expression \nare printed, you can use patterns like in the example below:"
7670
},
7771
{
78-
"cell_id" : 5079904273003789391,
79-
"cell_origin" : "client",
80-
"cell_type" : "input",
81-
"source" : "\\ket{A??}{q??}::LaTeXForm(\"|\", A??, \"\\rangle_{\", q??, \"}\")."
72+
"cell_id": 5079904273003789391,
73+
"cell_origin": "client",
74+
"cell_type": "input",
75+
"source": "\\ket{A??}{q??}::LaTeXForm(\"|\", A??, \"\\rangle_{\", q??, \"}\")."
8276
},
8377
{
84-
"cell_id" : 7082940046924425757,
85-
"cell_origin" : "client",
86-
"cell_type" : "input",
87-
"cells" :
88-
[
78+
"cell_id": 7082940046924425757,
79+
"cell_origin": "client",
80+
"cell_type": "input",
81+
"cells": [
8982
{
90-
"cell_id" : 2786973104563996210,
91-
"cell_origin" : "server",
92-
"cell_type" : "latex_view",
93-
"cells" :
94-
[
83+
"cell_id": 2786973104563996210,
84+
"cell_origin": "server",
85+
"cell_type": "latex_view",
86+
"cells": [
9587
{
96-
"cell_id" : 4241976921930736084,
97-
"cell_origin" : "server",
98-
"cell_type" : "input_form",
99-
"source" : "\\ket(n + 1 , a)"
88+
"cell_id": 4241976921930736084,
89+
"cell_origin": "server",
90+
"cell_type": "input_form",
91+
"source": "\\ket(n + 1 , a)"
10092
}
10193
],
102-
"source" : "\\begin{dmath*}{}|n+1\\rangle_{a}\\end{dmath*}"
94+
"source": "\\begin{dmath*}{}|n+1\\rangle_{a}\\end{dmath*}"
10395
}
10496
],
105-
"source" : "ex:=\\ket{n+1}{a};"
97+
"source": "ex:=\\ket{n+1}{a};"
10698
},
10799
{
108-
"cell_id" : 9144468563272193427,
109-
"cell_origin" : "client",
110-
"cell_type" : "latex",
111-
"cells" :
112-
[
100+
"cell_id": 9144468563272193427,
101+
"cell_origin": "client",
102+
"cell_type": "latex",
103+
"cells": [
113104
{
114-
"cell_id" : 2257828966897303850,
115-
"cell_origin" : "client",
116-
"cell_type" : "latex_view",
117-
"source" : "The comma-separated list of arguments to \\prop{LaTeXForm} will be concatenated\ninto a single LaTeX output string, with patterns replaced by their normal output\nform."
105+
"cell_id": 2257828966897303850,
106+
"cell_origin": "client",
107+
"cell_type": "latex_view",
108+
"source": "The comma-separated list of arguments to \\prop{LaTeXForm} will be concatenated\ninto a single LaTeX output string, with patterns replaced by their normal output\nform."
118109
}
119110
],
120-
"hidden" : true,
121-
"source" : "The comma-separated list of arguments to \\prop{LaTeXForm} will be concatenated\ninto a single LaTeX output string, with patterns replaced by their normal output\nform."
111+
"hidden": true,
112+
"source": "The comma-separated list of arguments to \\prop{LaTeXForm} will be concatenated\ninto a single LaTeX output string, with patterns replaced by their normal output\nform."
122113
},
123114
{
124-
"cell_id" : 105812070507528914,
125-
"cell_origin" : "client",
126-
"cell_type" : "input",
127-
"source" : ""
115+
"cell_id": 4022895960666784505,
116+
"cell_origin": "client",
117+
"cell_type": "latex",
118+
"cells": [
119+
{
120+
"cell_id": 5631816431885168242,
121+
"cell_origin": "client",
122+
"cell_type": "latex_view",
123+
"source": "Note that in all of the strings used inside the \\prop{LaTeXForm} property, you do not need to do any escaping of special characters: these strings are always interpreted as raw strings. Also note that you need to use double quotes, not single quotes."
124+
}
125+
],
126+
"hidden": true,
127+
"source": "Note that in all of the strings used inside the \\prop{LaTeXForm} property, you do not need to do any escaping of special characters: these strings are always interpreted as raw strings. Also note that you need to use double quotes, not single quotes."
128+
},
129+
{
130+
"cell_id": 386464782893588846,
131+
"cell_origin": "client",
132+
"cell_type": "input",
133+
"source": ""
128134
}
129135
],
130-
"description" : "Cadabra JSON notebook format",
131-
"version" : 1
136+
"description": "Cadabra JSON notebook format",
137+
"version": 1.0
132138
}

frontend/gtkmm/NotebookWindow.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,6 +1775,8 @@ bool NotebookWindow::cell_toggle_visibility(DTree::iterator it, int )
17751775
// Find the parent node. If that one is a latex cell, toggle visibility of
17761776
// the CodeInput widget (but not anything else in its vbox).
17771777

1778+
if(read_only) return false;
1779+
17781780
auto parent=DTree::parent(it);
17791781
if(parent->cell_type==DataCell::CellType::latex) {
17801782
// FIXME: we are not allowed to do this directly, all should go through
@@ -2438,6 +2440,8 @@ void NotebookWindow::on_edit_insert_above()
24382440

24392441
void NotebookWindow::on_edit_insert_below()
24402442
{
2443+
if(read_only) return;
2444+
24412445
if(current_cell==doc.end()) return;
24422446

24432447
DataCell newcell(DataCell::CellType::python, "");
@@ -2454,6 +2458,8 @@ void NotebookWindow::on_edit_insert_below()
24542458

24552459
void NotebookWindow::on_edit_delete()
24562460
{
2461+
if(read_only) return;
2462+
24572463
if(current_cell==doc.end()) return;
24582464

24592465
if(current_cell->running) return; // we are still expecting results, don't delete

frontend/gtkmm/TeXView.cc

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,8 @@ void TeXView::TeXArea::layout_latex() const
279279
owner->tex_error.emit(err.what());
280280

281281
// Instead put an error message in the output cell.
282+
std::cerr << err.what() << std::endl;
283+
std::cerr << fixed << std::endl;
282284
_render = microtex::MicroTeX::parse(
283285
"\\text{\\textcolor{red}{\\LaTeX error, probably mismatching brackets.}}", //microtex::utf82wide(fixed),
284286
rendering_width,
@@ -349,6 +351,17 @@ bool TeXView::TeXArea::on_draw(const Cairo::RefPtr<Cairo::Context>& cr)
349351
}
350352
}
351353

354+
std::string TeXView::TeXArea::verbatimise(const std::string& inp) const
355+
{
356+
std::string line;
357+
line = std::regex_replace(inp, std::regex(R"(\\)"), "\\,\\backslash\\! ");
358+
line = std::regex_replace(line, std::regex(R"(\{)"), "\\{");
359+
line = std::regex_replace(line, std::regex(R"(\})"), "\\}");
360+
line = std::regex_replace(line, std::regex(R"(_)"), "\\_");
361+
line = std::regex_replace(line, std::regex(R"(\~)"), "\\widetilde{~}");
362+
return line;
363+
}
364+
352365
void TeXView::TeXArea::set_latex(const std::string& latex)
353366
{
354367
// std::cout << "**** fixing latex " << latex << std::endl;
@@ -409,11 +422,7 @@ void TeXView::TeXArea::set_latex(const std::string& latex)
409422
}
410423
if(line.size()>0) {
411424
if(inverbatim) {
412-
line = std::regex_replace(line, std::regex(R"(\\)"), "\\backslash ");
413-
line = std::regex_replace(line, std::regex(R"(\{)"), "\\{");
414-
line = std::regex_replace(line, std::regex(R"(\})"), "\\}");
415-
line = std::regex_replace(line, std::regex(R"(_)"), "\\_");
416-
line = std::regex_replace(line, std::regex(R"(\~)"), "\\widetilde{~}");
425+
line = verbatimise(line);
417426
fixed += line+"\\\\";
418427
}
419428
else {
@@ -457,9 +466,6 @@ void TeXView::TeXArea::set_latex(const std::string& latex)
457466
fixed = std::regex_replace(fixed,
458467
std::regex(R"(\\prop\{([^\}]*)\})"),
459468
"\\texttt{$1}");
460-
fixed = std::regex_replace(fixed,
461-
std::regex(R"(\\verb\|([^\|]*)\|)"),
462-
"\\texttt{$1}");
463469
fixed = std::regex_replace(fixed,
464470
std::regex(R"(\\emph\{([^\}]*)\})"),
465471
"\\textit{$1}");
@@ -470,6 +476,21 @@ void TeXView::TeXArea::set_latex(const std::string& latex)
470476
std::regex(R"(\\end\{equation\*?\})"),
471477
"$");
472478

479+
// Finally, the inline verbatim.
480+
auto verb_regex = std::regex(R"(\\verb\|([^\|]*)\|)");
481+
std::sregex_iterator it(fixed.begin(), fixed.end(), verb_regex);
482+
std::sregex_iterator endit;
483+
size_t last_pos=0;
484+
std::string result;
485+
for (; it != endit; ++it) {
486+
result += fixed.substr(last_pos, it->position() - last_pos);
487+
std::string transformed = verbatimise(it->str(1));
488+
result += "\\texttt{" + transformed + "}";
489+
last_pos = it->position() + it->length();
490+
}
491+
if(last_pos < fixed.length())
492+
result += fixed.substr(last_pos);
493+
fixed=result;
473494

474495
fixed = "\\text{"+fixed+"}";
475496
}

frontend/gtkmm/TeXView.hh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ namespace cadabra {
9292
// MicroTeX
9393
mutable microtex::Render* _render;
9494
std::string unfixed, fixed;
95+
96+
// Return a string in which backslashes, curly braces,
97+
// underscores and tildes are properly backslashed, so they
98+
// typeset as if we are in a verbatim environment.
99+
std::string verbatimise(const std::string&) const;
95100
};
96101

97102
TeXArea image;

web2/cadabra2/source/changelog.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@ <h3>github devel branch (2.5.9)</h3>
3131
<li>Add preliminary support for Plotly as plotting backend.</li>
3232
<li>Word wrap LaTeX input cells to keep notebooks readable for any window width.</li>
3333
<li>Internal changes for python path handling.</li>
34+
<li>Bugfix for <tt>eliminate_kronecker</tt>.</li>
35+
<li>Fix for building Cadabra as a C++ library.</li>
36+
<li>Various UI and typesetting bug fixes.</li>
37+
<li>Fix for the homebrew package, and option to install the development branch using homebrew.</li>
3438
</ul>
3539

3640
<a name="2.5.8"></a>

web2/cadabra2/source/download.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ <h2>Binary packages</h2>
143143
do
144144
<code>brew tap kpeeters/repo</code>
145145
<code>brew install cadabra2</code>
146+
Alternatively, if you want to live on the bleeding edge and
147+
want to run the current development branch, install <tt>cadabra2-devel</tt>.
146148
If you encounter problems, please
147149
email <a href="mailto:[email protected]">[email protected]</a>
148150
so they can be fixed and other people can benefit from it.

0 commit comments

Comments
 (0)