Skip to content

Commit cbcc9a7

Browse files
committed
adding new cell-level REFORMAT key: "escape" for LaTeX special characters
1 parent 2f725ab commit cbcc9a7

4 files changed

Lines changed: 52 additions & 24 deletions

File tree

src/latex.cpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ class SpLatex
6666
return m_lines.scan_for_comment<comment_char>(ln, cprefix);
6767
}
6868

69-
//! escape special latex characters
70-
static inline std::string
71-
escape(const std::string& str);
72-
7369
//! Process % SQL commands
7470
void sql(size_t ln, size_t indent, const std::string& cmdline);
7571

@@ -104,23 +100,6 @@ class SpLatex
104100
SpLatex(TextLines& lines);
105101
};
106102

107-
//! escape special latex characters
108-
inline std::string SpLatex::escape(const std::string& str)
109-
{
110-
std::string out;
111-
out.reserve(str.size());
112-
for (std::string::const_iterator s = str.begin(); s != str.end(); ++s)
113-
{
114-
if (*s == '#' || *s == '$' || *s == '%' || *s == '^' || *s == '&' ||
115-
*s == '{' || *s == '}' || *s == '_' || *s == '~' || *s == '\\')
116-
{
117-
out += '\\';
118-
}
119-
out += *s;
120-
}
121-
return out;
122-
}
123-
124103
//! Process % SQL commands
125104
void SpLatex::sql(size_t /* ln */, size_t /* indent */, const std::string& cmdline)
126105
{
@@ -295,7 +274,8 @@ void SpLatex::multiplot(size_t ln, size_t indent, const std::string& cmdline)
295274
std::ostringstream os;
296275
for (size_t i = 0; i < groupcols.size(); ++i) {
297276
if (i != 0) os << ',';
298-
os << escape(groupfields[i]) << '=' << escape(rowgroup[i]);
277+
os << escape_latex(groupfields[i])
278+
<< '=' << escape_latex(rowgroup[i]);
299279
}
300280
legendlist.push_back(os.str());
301281
}

src/reformat.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,12 @@ bool Reformat::Cell::parse_keyformat(const std::string& key,
256256
std::string::const_iterator& curr,
257257
const std::string::const_iterator& end)
258258
{
259-
if (key == "floor")
259+
if (key == "escape")
260+
{
261+
m_escape = true;
262+
return true;
263+
}
264+
else if (key == "floor")
260265
{
261266
m_round = RD_FLOOR;
262267
return true;
@@ -742,6 +747,26 @@ std::string Reformat::format(int row, int col, const std::string& in_text) const
742747
text = "\\emph{" + text + "}";
743748
}
744749
}
750+
else
751+
{
752+
Line fmt = m_fmt;
753+
754+
{
755+
linefmt_type::const_iterator rowfmt = m_rowfmt.find(row);
756+
if (rowfmt != m_rowfmt.end())
757+
fmt.apply(rowfmt->second);
758+
759+
linefmt_type::const_iterator colfmt = m_colfmt.find(col);
760+
if (colfmt != m_colfmt.end())
761+
fmt.apply(colfmt->second);
762+
}
763+
764+
if (fmt.m_escape)
765+
{
766+
// escape special LaTeX characters
767+
text = escape_latex(text);
768+
}
769+
}
745770

746771
return text;
747772
}

src/reformat.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* Reformatting styles for text or double value cells.
3535
*
3636
* cell-level formats:
37+
* - escape (escape special LaTeX characters)
3738
* - round=# (# = 'floor', 'ceil' or number of decimal digits)
3839
* - precision=# (# = show decimal digits)
3940
* - width=# (# = set width of output)
@@ -87,6 +88,9 @@ class Reformat
8788
//! Specifications for cell-level formats
8889
struct Cell
8990
{
91+
//! escape special LaTeX characters
92+
bool m_escape;
93+
9094
//! round double numbers, options: floor, ceil, round, precision
9195
enum { RD_UNDEF, RD_FLOOR, RD_CEIL, RD_ROUND, RD_DIGITS } m_round;
9296

@@ -107,7 +111,8 @@ class Reformat
107111

108112
//! Initialize with sentinels
109113
Cell()
110-
: m_round(RD_UNDEF),
114+
: m_escape(false),
115+
m_round(RD_UNDEF),
111116
m_round_digits(-1),
112117
m_reformat_precision(-1),
113118
m_reformat_width(-1),

src/strtools.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,4 +385,22 @@ simple_diff(const std::string& strA, const std::string& strB,
385385
}
386386
}
387387

388+
//! escape special latex characters
389+
static inline std::string
390+
escape_latex(const std::string& str)
391+
{
392+
std::string out;
393+
out.reserve(str.size());
394+
for (std::string::const_iterator s = str.begin(); s != str.end(); ++s)
395+
{
396+
if (*s == '#' || *s == '$' || *s == '%' || *s == '^' || *s == '&' ||
397+
*s == '{' || *s == '}' || *s == '_' || *s == '~' || *s == '\\')
398+
{
399+
out += '\\';
400+
}
401+
out += *s;
402+
}
403+
return out;
404+
}
405+
388406
#endif // STRTOOLS_HEADER

0 commit comments

Comments
 (0)