Skip to content

Commit aa63a3e

Browse files
committed
Tex: parse \newtheorem
1 parent bcf90ed commit aa63a3e

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
boxed input.tex /^\\newenvironment{boxed}$/;" e
22
boxedA input.tex /^\\renewenvironment{boxedA}$/;" e
3+
theorem input.tex /^\\newtheorem{theorem}{Theorem}$/;" t

Units/parser-tex.r/newenvironment.d/input.tex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,15 @@
1919
\end{tabular}
2020
\end{center}
2121
}
22+
\newtheorem{theorem}{Theorem}
2223
\begin{document}
2324
\begin{boxed}
2425
foo
2526
\end{boxed}
2627
\begin{boxedA}
2728
foo
2829
\end{boxedA}
30+
\begin{theorem}
31+
bar
32+
\end{theorem}
2933
\end{document}

parsers/tex.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ enum eKeywordId {
6969
KEYWORD_declaremathoperator,
7070
KEYWORD_newenvironment,
7171
KEYWORD_renewenvironment,
72+
KEYWORD_newtheorem,
7273
KEYWORD_newcounter,
7374
};
7475
typedef int keywordId; /* to allow KEYWORD_NONE */
@@ -125,6 +126,7 @@ typedef enum {
125126
TEXTAG_COMMAND,
126127
TEXTAG_OPERATOR,
127128
TEXTAG_ENVIRONMENT,
129+
TEXTAG_THEOREM,
128130
TEXTAG_COUNTER,
129131
TEXTAG_COUNT
130132
} texKind;
@@ -159,6 +161,7 @@ static kindDefinition TexKinds [] = {
159161
{ true, 'C', "command", "command created with \\newcommand" },
160162
{ true, 'o', "operator", "math operator created with \\DeclareMathOperator" },
161163
{ true, 'e', "environment", "environment created with \\newenvironment" },
164+
{ true, 't', "theorem", "theorem created with \\newtheorem" },
162165
{ true, 'N', "counter", "counter created with \\newcounter" },
163166
};
164167

@@ -185,6 +188,7 @@ static const keywordTable TexKeywordTable [] = {
185188
{ "DeclareMathOperator", KEYWORD_declaremathoperator },
186189
{ "newenvironment", KEYWORD_newenvironment },
187190
{ "renewenvironment", KEYWORD_renewenvironment},
191+
{ "newtheorem", KEYWORD_newtheorem },
188192
{ "newcounter", KEYWORD_newcounter },
189193
};
190194

@@ -940,6 +944,50 @@ static bool parseNewEnvironment (tokenInfo *const token, bool *tokenUnprocessed)
940944
return eof;
941945
}
942946

947+
static bool parseNewTheorem (tokenInfo *const token, bool *tokenUnprocessed)
948+
{
949+
bool eof = false;
950+
/* \newtheorem{name}{title}
951+
\newtheorem{name}{title}[numbered_within]
952+
\newtheorem{name}[numbered_like]{title} */
953+
struct TexParseStrategy strategy [] = {
954+
{
955+
.type = '{',
956+
.flags = 0,
957+
.kindIndex = TEXTAG_THEOREM,
958+
.roleIndex = ROLE_DEFINITION_INDEX,
959+
.name = NULL,
960+
.unique = false,
961+
},
962+
{
963+
.type = '[',
964+
.flags = TEX_NAME_FLAG_OPTIONAL,
965+
.kindIndex = KIND_GHOST_INDEX,
966+
.name = NULL,
967+
},
968+
{
969+
.type = '{',
970+
.flags = 0,
971+
.kindIndex = KIND_GHOST_INDEX,
972+
.name = NULL,
973+
},
974+
{
975+
.type = '[',
976+
.flags = TEX_NAME_FLAG_OPTIONAL,
977+
.kindIndex = KIND_GHOST_INDEX,
978+
.name = NULL,
979+
},
980+
{
981+
.type = 0
982+
}
983+
};
984+
985+
if (parseWithStrategy (token, strategy, tokenUnprocessed))
986+
eof = true;
987+
988+
return eof;
989+
}
990+
943991
static bool parseNewcounter (tokenInfo *const token, bool *tokenUnprocessed)
944992
{
945993
bool eof = false;
@@ -1048,6 +1096,9 @@ static void parseTexFile (tokenInfo *const token)
10481096
case KEYWORD_renewenvironment:
10491097
eof = parseNewEnvironment (token, &tokenUnprocessed);
10501098
break;
1099+
case KEYWORD_newtheorem:
1100+
eof = parseNewTheorem (token, &tokenUnprocessed);
1101+
break;
10511102
case KEYWORD_newcounter:
10521103
eof = parseNewcounter (token, &tokenUnprocessed);
10531104
break;

0 commit comments

Comments
 (0)