Skip to content

Commit e5f724f

Browse files
committed
Formula doesn't render due to empty lines.
When having a formula like: ``` @f[ A = \left(\begin{array}{cc} Ixx & Ixy \\ Ixy & Iyy \\ \end{array}\right) @f] ``` this doesn't render in LaTeX but renders under MathJax. The problem is the starting and ending empty lines inside the formula. Stripping away the starting and ending empty lines.
1 parent 9b51550 commit e5f724f

2 files changed

Lines changed: 48 additions & 14 deletions

File tree

src/commentscan.l

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,8 @@ struct commentscanYY_state
462462
OutputContext inContext; // are we inside the brief, details or xref part
463463
bool briefEndsAtDot = FALSE; // does the brief description stop at a dot?
464464
QCString formulaText; // Running text of a formula
465+
QCString formulaPreText; // Start command of a formula
466+
QCString formulaPostText; // End command of a formula
465467
QCString formulaEnv; // environment name
466468
int formulaNewLines = 0; // amount of new lines in the formula
467469
QCString *pOutputString = nullptr; // pointer to string to which the output is appended.
@@ -993,30 +995,38 @@ STopt [^\n@\\]*
993995
}
994996
<Comment>{B}*{CMD}"f{"[^}\n]+"}"("{"?) { // start of a formula with custom environment
995997
setOutput(yyscanner,OutputDoc);
996-
yyextra->formulaText="\\begin";
998+
yyextra->formulaText="";
999+
yyextra->formulaPreText="\\begin";
1000+
yyextra->formulaPostText="";
9971001
yyextra->formulaEnv=QCString(yytext).stripWhiteSpace().mid(2);
9981002
if (yyextra->formulaEnv.at(yyextra->formulaEnv.length()-1)=='{')
9991003
{
10001004
// remove trailing open brace
10011005
yyextra->formulaEnv=yyextra->formulaEnv.left(yyextra->formulaEnv.length()-1);
10021006
}
1003-
yyextra->formulaText+=yyextra->formulaEnv;
1007+
yyextra->formulaPreText+=yyextra->formulaEnv;
10041008
yyextra->formulaNewLines=0;
10051009
BEGIN(ReadFormulaLong);
10061010
}
10071011
<Comment>{B}*{CMD}"f$" { // start of a inline formula
1008-
yyextra->formulaText="$";
1012+
yyextra->formulaText="";
1013+
yyextra->formulaPreText="$";
1014+
yyextra->formulaPostText="";
10091015
yyextra->formulaNewLines=0;
10101016
BEGIN(ReadFormulaShort);
10111017
}
10121018
<Comment>{B}*{CMD}"f(" { // start of a inline formula
10131019
yyextra->formulaText="";
1020+
yyextra->formulaPreText="";
1021+
yyextra->formulaPostText="";
10141022
yyextra->formulaNewLines=0;
10151023
BEGIN(ReadFormulaRound);
10161024
}
10171025
<Comment>{B}*{CMD}"f[" { // start of a block formula
10181026
setOutput(yyscanner,OutputDoc);
1019-
yyextra->formulaText="\\[";
1027+
yyextra->formulaText="";
1028+
yyextra->formulaPreText="\\[";
1029+
yyextra->formulaPostText="";
10201030
yyextra->formulaNewLines=0;
10211031
BEGIN(ReadFormulaLong);
10221032
}
@@ -1201,7 +1211,7 @@ STopt [^\n@\\]*
12011211
/* -------------- Rules for handling formulas ---------------- */
12021212

12031213
<ReadFormulaShort,ReadFormulaShortSection>{CMD}"f$" { // end of inline formula
1204-
yyextra->formulaText+="$";
1214+
yyextra->formulaPostText+="$";
12051215
QCString form = addFormula(yyscanner);
12061216
addOutput(yyscanner," "+form);
12071217
if (YY_START == ReadFormulaShort)
@@ -1228,13 +1238,13 @@ STopt [^\n@\\]*
12281238
}
12291239
}
12301240
<ReadFormulaLong>{CMD}"f]" { // end of block formula
1231-
yyextra->formulaText+="\\]";
1241+
yyextra->formulaPostText+="\\]";
12321242
addOutput(yyscanner," "+addFormula(yyscanner));
12331243
BEGIN(Comment);
12341244
}
12351245
<ReadFormulaLong>{CMD}"f}" { // end of custom env formula
1236-
yyextra->formulaText+="\\end";
1237-
yyextra->formulaText+=yyextra->formulaEnv;
1246+
yyextra->formulaPostText+="\\end";
1247+
yyextra->formulaPostText+=yyextra->formulaEnv;
12381248
addOutput(yyscanner," "+addFormula(yyscanner));
12391249
BEGIN(Comment);
12401250
}
@@ -1840,12 +1850,16 @@ STopt [^\n@\\]*
18401850
BEGIN( Comment );
18411851
}
18421852
<SectionTitle>{B}*{CMD}"f$" {
1843-
yyextra->formulaText="$";
1853+
yyextra->formulaText="";
1854+
yyextra->formulaPreText="$";
1855+
yyextra->formulaPostText="";
18441856
yyextra->formulaNewLines=0;
18451857
BEGIN(ReadFormulaShortSection);
18461858
}
18471859
<SectionTitle>{B}*{CMD}"f(" { // start of a inline formula
18481860
yyextra->formulaText="";
1861+
yyextra->formulaPreText="";
1862+
yyextra->formulaPostText="";
18491863
yyextra->formulaNewLines=0;
18501864
BEGIN(ReadFormulaRoundSection);
18511865
}
@@ -4102,6 +4116,18 @@ static void addXRefItem(yyscan_t yyscanner,
41024116
}
41034117

41044118
//-----------------------------------------------------------------------------
4119+
inline bool qqisspace(char c)
4120+
{ return c==' ' || c=='\t'; }
4121+
4122+
QCString qStripEndWhiteSpace(QCString rep)
4123+
{
4124+
size_t sl = rep.length();
4125+
if (sl==0 || (!qqisspace(rep[sl-1]))) return rep;
4126+
size_t start=0,end=sl-1;
4127+
while (end>start && qqisspace(rep[end])) end--;
4128+
return QCString(rep.mid(start,1+end-start));
4129+
}
4130+
41054131

41064132
// Adds a formula text to the list/dictionary of formulas if it was
41074133
// not already added. Returns the label of the formula.
@@ -4110,7 +4136,10 @@ static QCString addFormula(yyscan_t yyscanner)
41104136
std::unique_lock<std::mutex> lock(g_formulaMutex);
41114137
struct yyguts_t *yyg = (struct yyguts_t*)yyscanner;
41124138
QCString formLabel;
4113-
int id = FormulaManager::instance().addFormula(yyextra->formulaText.str());
4139+
int cnt = 0;
4140+
QCString stripForm = stripLeadingAndTrailingEmptyLines(yyextra->formulaText,cnt);
4141+
if (yyextra->formulaText.stripWhiteSpace(true).endsWith("\n")) stripForm += "\n";
4142+
int id = FormulaManager::instance().addFormula((yyextra->formulaPreText + stripForm + yyextra->formulaPostText).str());
41144143
formLabel.sprintf("\\_form#%d",id);
41154144
for (int i=0;i<yyextra->formulaNewLines;i++) formLabel+="@_fakenl"; // add fake newlines to
41164145
// keep the warnings

src/qcstring.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ inline int qstrncmp( const char *str1, const char *str2, size_t len )
8181
inline bool qisspace(char c)
8282
{ return c==' ' || c=='\t' || c=='\n' || c=='\r'; }
8383

84+
inline bool qisrealspace(char c)
85+
{ return c==' ' || c=='\t'; }
86+
8487
int qstricmp( const char *str1, const char *str2 );
8588

8689
int qstrnicmp( const char *str1, const char *str2, size_t len );
@@ -241,14 +244,16 @@ class QCString
241244
}
242245

243246
/// returns a copy of this string with leading and trailing whitespace removed
244-
QCString stripWhiteSpace() const
247+
QCString stripWhiteSpace(bool realWhiteSpace = false) const
245248
{
246249
size_t sl = m_rep.size();
247-
if (sl==0 || (!qisspace(m_rep[0]) && !qisspace(m_rep[sl-1]))) return *this;
250+
bool (*whiteSpaceFie)(char c) = qisspace;
251+
if (realWhiteSpace) whiteSpaceFie =qisrealspace;
252+
if (sl==0 || (!whiteSpaceFie(m_rep[0]) && !whiteSpaceFie(m_rep[sl-1]))) return *this;
248253
size_t start=0,end=sl-1;
249-
while (start<sl && qisspace(m_rep[start])) start++;
254+
while (start<sl && whiteSpaceFie(m_rep[start])) start++;
250255
if (start==sl) return QCString(); // only whitespace
251-
while (end>start && qisspace(m_rep[end])) end--;
256+
while (end>start && whiteSpaceFie(m_rep[end])) end--;
252257
return QCString(m_rep.substr(start,1+end-start));
253258
}
254259

0 commit comments

Comments
 (0)