Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Attempting to fix the curly brackets bug #166

Merged
merged 11 commits into from
Nov 4, 2023
7 changes: 1 addition & 6 deletions src/mminou.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ void printLongLine(const char *line, const char *startNextLine, const char *brea
vstring_def(prefix);
vstring_def(startNextLine1);
vstring_def(breakMatch1);
long i, j, p;
long i, p;
long startNextLineLen;
flag firstLine;
flag tildeFlag = 0;
Expand Down Expand Up @@ -559,11 +559,6 @@ void printLongLine(const char *line, const char *startNextLine, const char *brea
// where all ASCII 3's are converted back to space.
// Note added 20-Oct-02: tidy.exe breaks HREF quotes with new line.
// Check HTML spec - do we really need this code?
j = (long)strlen(multiLine);
// Do a bug check to make sure no real ASCII 3's are ever printed
for (i = 0; i < j; i++) {
if (multiLine[i] == QUOTED_SPACE) bug(1514); // Should never be the case
}
if (breakMatch1[0] == '\"') {
breakMatch1[0] = ' '; // Change to a space (the real break character)
// Scan string for quoted strings
Expand Down
147 changes: 98 additions & 49 deletions src/mmwtex.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ pntrString_def(g_mathboxUser); // User name vs. mathbox #
// in a math string in a comment to be removed for HTML output.
#define CLOSING_PUNCTUATION ".,;)?!:]'\"_-"

/*!
* \def QUOTED_SPACE
* The general line wrapping algorithm looks out for spaces as break positions.
* To prevent a quote delimited by __"__ be broken down, spaces are temporarily
* replaced with 0x03 (ETX, end of transmission), hopefully never used in
* text in this application.
*/
#define QUOTED_SPACE 3 // ASCII 3 that temporarily zaps a space

// Tex output file
FILE *g_texFilePtr = NULL;
flag g_texFileOpenFlag = 0;
Expand Down Expand Up @@ -2806,10 +2815,11 @@ void printTexLongMath(nmbrString *mathString,
long indentationLevel)
{
#define INDENTATION_OFFSET 1
long i;
long i, j, k, n;
long pos;
vstring_def(tex);
vstring_def(texLine);
vstring_def(texFull);
vstring_def(sPrefix);
vstring_def(htmStep);
vstring_def(htmStepTag);
Expand Down Expand Up @@ -3047,55 +3057,94 @@ void printTexLongMath(nmbrString *mathString,
if (!g_oldTexFlag) {
if (refType == 'e' || refType == 'f') {
// A hypothesis - don't include \ref{}
printLongLine(cat(" ",
// If not first step, so print "\\" LaTeX line break
!strcmp(htmStep, "1") ? "" : "\\\\ ",
htmStep, // Step number
" && ",
" & ",
texLine,
// Don't put space to help prevent bad line break
"&\\text{Hyp~",
// The following puts a hypothesis number such as "2" if
// $e label is "abc.2"; if no ".", will be whole label.
right(htmRef, instr(1, htmRef, ".") + 1),
"}\\notag%",
// Add full label as LaTeX comment - note lack of space after
// "%" above to prevent bad line break.
htmRef, NULL),
" \\notag \\\\ && & \\qquad ", // Continuation line prefix
" ");
texFull = cat(" ", // texFull[0] should not be a "{" character.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I added a comment to point out that texFull[0] = " " should not be a "{" character, since it wouldn't be checked by the subsequent counting algorithm.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, and here it obviously will never be "{", since it is a blank space " ".

// If not first step, so print "\\" LaTeX line break
!strcmp(htmStep, "1") ? "" : "\\\\ ",
htmStep, // Step number
" && ",
" & ",
texLine,
// Don't put space to help prevent bad line break
"&\\text{Hyp~",
// The following puts a hypothesis number such as "2" if
// $e label is "abc.2"; if no ".", will be whole label.
right(htmRef, instr(1, htmRef, ".") + 1),
"}\\notag%",
// Add full label as LaTeX comment - note lack of space after
// "%" above to prevent bad line break.
htmRef, NULL),

// To avoid generating incorrect TeX, line breaking is forbidden inside
// scopes of curly braces. However breaking between '\{' and '\}' is
// allowed.
// The spaces that should not be matched with 'breakMatch' are
// temporarily changed to ASCII 3 before the 'printLongLine' procedure
// is called. The procedure rewraps the 'line' argument matching the
// unchanged spaces only, thus ensuring bad breaks will be avoided.
// The reverse is done in the 'print2()' function, where all ASCII 3's
// are converted back to spaces.
// k counts the scope level we are in.
k = 0;
n = (long)strlen(texFull);
for (j = 1; j < n; j++) { // We don't need to check texFull[0].
Copy link
Contributor Author

@GinoGiotto GinoGiotto Oct 25, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here I wasn't sure what apporach would be best. I defined the texFull string as the line that is feeded to the printLongLine function. The checking cicle starts at the second character of texFull to avoid checking the unorthodox texFull[-1] != '\\'. The first character is texFull[0] = " " so we don't need to check it since it's fixed.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.

// We enter a non "\{" scope.
if (texFull[j] == '{' && texFull[j - 1] != '\\') k++;
// We escape a non "\}" scope.
if (texFull[j] == '}' && texFull[j - 1] != '\\') k--;
// If k > 0 then we are inside a scope.
if (texFull[j] == ' ' && k > 0) texFull[j] = QUOTED_SPACE;
}
printLongLine(texFull, " \\notag \\\\ && & \\qquad ", /* Continuation line prefix */ " ");
} else {
printLongLine(cat(" ",
// If not first step, so print "\\" LaTeX line break
!strcmp(htmStep, "1") ? "" : "\\\\ ",
htmStep, // Step number
" && ",

// Local label if any e.g. "@2:"
(htmLocLab[0] != 0) ? cat(htmLocLab, "\\ ", NULL) : "",

" & ",
texLine,
// Don't put space to help prevent bad line break

// Surround \ref with \mbox for non-math-mode
// symbolic labels (due to \tag{..} in mmcmds.c). Also,
// move hypotheses to after referenced label.
"&",
"(",

// Don't make local label a \ref
(htmRef[0] != '@') ?
cat("\\mbox{\\ref{eq:", htmRef, "}}", NULL)
: htmRef,

htmHyp[0] ? "," : "",
htmHyp,
")\\notag", NULL),

" \\notag \\\\ && & \\qquad ", // Continuation line prefix
" ");
texFull = cat(" ", // texFull[0] should not be a "{" character.
// If not first step, so print "\\" LaTeX line break
!strcmp(htmStep, "1") ? "" : "\\\\ ",
htmStep, // Step number
" && ",

// Local label if any e.g. "@2:"
(htmLocLab[0] != 0) ? cat(htmLocLab, "\\ ", NULL) : "",

" & ",
texLine,
// Don't put space to help prevent bad line break

// Surround \ref with \mbox for non-math-mode
// symbolic labels (due to \tag{..} in mmcmds.c). Also,
// move hypotheses to after referenced label.
"&",
"(",

// Don't make local label a \ref
(htmRef[0] != '@') ?
cat("\\mbox{\\ref{eq:", htmRef, "}}", NULL)
: htmRef,

htmHyp[0] ? "," : "",
htmHyp,
")\\notag", NULL),

// To avoid generating incorrect TeX, line breaking is forbidden inside
// scopes of curly braces. However breaking between '\{' and '\}' is
// allowed.
// The spaces that should not be matched with 'breakMatch' are
// temporarily changed to ASCII 3 before the 'printLongLine' procedure
// is called. The procedure rewraps the 'line' argument matching the
// unchanged spaces only, thus ensuring bad breaks will be avoided.
// The reverse is done in the 'print2()' function, where all ASCII 3's
// are converted back to spaces.
// k counts the scope level we are in.
k = 0;
n = (long)strlen(texFull);
for (j = 1; j < n; j++) { // We don't need to check texFull[0].
// We enter a non "\{" scope.
if (texFull[j] == '{' && texFull[j - 1] != '\\') k++;
// We escape a non "\}" scope.
if (texFull[j] == '}' && texFull[j - 1] != '\\') k--;
// If k > 0 then we are inside a scope.
if (texFull[j] == ' ' && k > 0) texFull[j] = QUOTED_SPACE;
}
printLongLine(texFull, " \\notag \\\\ && & \\qquad ", /* Continuation line prefix */ " ");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like a lot of duplication with the previous part.
Then again the duplicated code was already there before.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I unduplicated the brace count and the printLongLine call in a new commit.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

}
} else {
printLongLine(texLine, "", "\\");
Expand Down
Loading