Skip to content

Commit 8c98d09

Browse files
committed
Replace Greek letters entered as unicode instead of TeX
People doing quick and dirty things might want to create a new expression by copying and pasting from the output of a previous algorithm. The asymmetry here is that input typically has TeX while output typically has unicode. We can solve this by performing a replacement in the parser. It is then natural to copy greekmap from DisplayTerminal to Symbols (and add varphi to it). In case there are users who input both TeX and unicode and rely on them being distinct, this could be put behind a command-line argument or environment variable. Signed-off-by: Connor Behan <[email protected]>
1 parent caaf519 commit 8c98d09

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

core/Parser.cc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ You should have received a copy of the GNU General Public License
2020

2121
#include "Parser.hh"
2222
#include "PreProcessor.hh"
23+
#include "Symbols.hh"
2324

2425
#include <sstream>
2526
#include <internal/uniconv.h>
@@ -178,6 +179,17 @@ bool Parser::string2tree(const std::string& inp)
178179
ss2 << pp;
179180
std::string str8=" "+ss2.str()+" "; // for lookahead
180181

182+
#ifdef DEBUG
183+
std::cout << "converting Greek unicode to TeX" << std::endl;
184+
#endif
185+
for (auto const& c : cadabra::symbols::greekmap) {
186+
size_t pos1 = 0;
187+
size_t pos2;
188+
while ((pos2 = str8.find(c.second, pos1)) != std::string::npos) {
189+
str8.replace(pos2, c.second.length(), c.first);
190+
pos1 = pos2 + c.first.length();
191+
}
192+
}
181193
#ifdef DEBUG
182194
std::cout << "converting to utf32" << std::endl;
183195
#endif

core/Symbols.cc

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,55 @@ const std::set<std::string> cadabra::symbols::greek {
5454
"\\Omega"
5555
};
5656

57+
const std::map<std::string, std::string> cadabra::symbols::greekmap {
58+
{"\\alpha", "α" },
59+
{"\\beta", "β" }, // beta seems to be reserved
60+
{"\\gamma", "γ" }, // gamma seems to be reserved
61+
{"\\delta", "δ" },
62+
{"\\epsilon", "ε" },
63+
{"\\zeta", "ζ" },
64+
{"\\eta", "η" },
65+
{"\\theta", "θ" },
66+
{"\\iota", "ι" },
67+
{"\\kappa", "κ" },
68+
{"\\lambda", "λ" }, // lambda is reserved
69+
{"\\mu", "μ" },
70+
{"\\nu", "ν" },
71+
{"\\xi", "ξ" },
72+
{"\\omicron", "ο" },
73+
{"\\pi", "π" },
74+
{"\\rho", "ρ" },
75+
{"\\sigma", "σ" },
76+
{"\\tau", "τ" },
77+
{"\\upsilon", "υ" },
78+
{"\\phi", "φ" },
79+
{"\\varphi", "ϕ" },
80+
{"\\chi", "χ" },
81+
{"\\psi", "ψ" },
82+
{"\\omega", "ω" },
83+
84+
{"\\Alpha", "Α" },
85+
{"\\Beta", "Β" },
86+
{"\\Gamma", "Γ" },
87+
{"\\Delta", "Δ" },
88+
{"\\Epsilon", "Ε" },
89+
{"\\Zeta", "Ζ" },
90+
{"\\Eta", "Η" },
91+
{"\\Theta", "ϴ" },
92+
{"\\Iota", "Ι" },
93+
{"\\Kappa", "Κ" },
94+
{"\\Lambda", "Λ" },
95+
{"\\Mu", "Μ" },
96+
{"\\Nu", "Ν" },
97+
{"\\Xi", "Ξ" },
98+
{"\\Omicron", "Ο" },
99+
{"\\Pi", "Π" },
100+
{"\\Rho", "Ρ" },
101+
{"\\Sigma", "Σ" },
102+
{"\\Tau", "Τ" },
103+
{"\\Upsilon", "Υ" },
104+
{"\\Phi", "Φ" },
105+
{"\\Chi", "Χ" },
106+
{"\\Psi", "Ψ" },
107+
{"\\Omega", "Ω" },
108+
};

core/Symbols.hh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22
#pragma once
33

44
#include <set>
5+
#include <map>
56
#include <string>
67

78
namespace cadabra {
89
namespace symbols {
910
extern const std::set<std::string> greek;
11+
extern const std::map<std::string, std::string> greekmap;
1012
}
1113
}

0 commit comments

Comments
 (0)