latex.grammar 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. // Track environments
  2. @context elementContext from "./tokens.mjs"
  3. // External tokens must be defined before normal @tokens to take precedence
  4. // over them.
  5. @external tokens verbTokenizer from "./tokens.mjs" {
  6. VerbContent
  7. }
  8. @external tokens lstinlineTokenizer from "./tokens.mjs" {
  9. LstInlineContent
  10. }
  11. @external tokens literalArgTokenizer from "./tokens.mjs" {
  12. LiteralArgContent
  13. }
  14. @external tokens spaceDelimitedLiteralArgTokenizer from "./tokens.mjs" {
  15. SpaceDelimitedLiteralArgContent
  16. }
  17. @external tokens verbatimTokenizer from "./tokens.mjs" {
  18. VerbatimContent
  19. }
  20. @external tokens mathDelimiterTokenizer from "./tokens.mjs" {
  21. MathDelimiter
  22. }
  23. // external tokenizer to read control sequence names including @ signs
  24. // (which are often used in TeX definitions).
  25. @external tokens csnameTokenizer from "./tokens.mjs" {
  26. Csname
  27. }
  28. @external tokens trailingContentTokenizer from "./tokens.mjs" {
  29. TrailingWhitespaceOnly,
  30. TrailingContent
  31. }
  32. // It doesn't seem to be possible to access specialized tokens in the context tracker.
  33. // They have id's which are not exported in the latex.terms.js file.
  34. // This is a workaround: use an external specializer to explicitly choose the terms
  35. // to use for the specialized tokens.
  36. @external specialize {CtrlSeq} specializeCtrlSeq from "./tokens.mjs" {
  37. Begin,
  38. End,
  39. RefCtrlSeq,
  40. RefStarrableCtrlSeq,
  41. CiteCtrlSeq,
  42. CiteStarrableCtrlSeq,
  43. LabelCtrlSeq,
  44. MathTextCtrlSeq,
  45. HboxCtrlSeq,
  46. TitleCtrlSeq,
  47. DocumentClassCtrlSeq,
  48. UsePackageCtrlSeq,
  49. HrefCtrlSeq,
  50. UrlCtrlSeq,
  51. VerbCtrlSeq,
  52. LstInlineCtrlSeq,
  53. IncludeGraphicsCtrlSeq,
  54. CaptionCtrlSeq,
  55. DefCtrlSeq,
  56. LeftCtrlSeq,
  57. RightCtrlSeq,
  58. NewCommandCtrlSeq,
  59. RenewCommandCtrlSeq,
  60. NewEnvironmentCtrlSeq,
  61. RenewEnvironmentCtrlSeq,
  62. // services/web/frontend/js/features/outline/outline-parser.js
  63. BookCtrlSeq,
  64. PartCtrlSeq,
  65. ChapterCtrlSeq,
  66. SectionCtrlSeq,
  67. SubSectionCtrlSeq,
  68. SubSubSectionCtrlSeq,
  69. ParagraphCtrlSeq,
  70. SubParagraphCtrlSeq,
  71. InputCtrlSeq,
  72. IncludeCtrlSeq,
  73. ItemCtrlSeq,
  74. NewTheoremCtrlSeq,
  75. TheoremStyleCtrlSeq,
  76. CenteringCtrlSeq,
  77. BibliographyCtrlSeq,
  78. BibliographyStyleCtrlSeq,
  79. AuthorCtrlSeq,
  80. AffilCtrlSeq,
  81. AffiliationCtrlSeq,
  82. MaketitleCtrlSeq,
  83. TextColorCtrlSeq,
  84. ColorBoxCtrlSeq,
  85. HLineCtrlSeq,
  86. TopRuleCtrlSeq,
  87. MidRuleCtrlSeq,
  88. BottomRuleCtrlSeq,
  89. MultiColumnCtrlSeq
  90. }
  91. @external specialize {EnvName} specializeEnvName from "./tokens.mjs" {
  92. DocumentEnvName,
  93. TabularEnvName,
  94. EquationEnvName,
  95. EquationArrayEnvName,
  96. VerbatimEnvName,
  97. TikzPictureEnvName,
  98. FigureEnvName,
  99. ListEnvName,
  100. TableEnvName
  101. }
  102. @external specialize {CtrlSym} specializeCtrlSym from "./tokens.mjs" {
  103. OpenParenCtrlSym,
  104. CloseParenCtrlSym,
  105. OpenBracketCtrlSym,
  106. CloseBracketCtrlSym,
  107. LineBreakCtrlSym
  108. }
  109. @tokens {
  110. CtrlSeq { "\\" $[a-zA-Z]+ }
  111. CtrlSym { "\\" ![a-zA-Z] }
  112. // tokens for paragraphs
  113. Whitespace { $[ \t]+ }
  114. NewLine { "\n" }
  115. BlankLine { "\n" "\n"+ }
  116. Normal { ![\\{}\[\]$&~#^_% \t\n] ![\\{}\[\]$&~#^_%\t\n]* } // everything is normal text, except these characters
  117. @precedence { CtrlSeq, CtrlSym, BlankLine, NewLine, Whitespace, Normal }
  118. OpenBrace[closedBy=CloseBrace] { "{" }
  119. CloseBrace[openedBy=OpenBrace] { "}" }
  120. OpenBracket[closedBy=CloseBracket] { "[" }
  121. CloseBracket[openedBy=OpenBracket] { "]" }
  122. Comment { "%" ![\n]* "\n"? }
  123. Dollar { "$" }
  124. Number { $[0-9]+ ("." $[0-9]*)? }
  125. MathSpecialChar { $[^_=<>()\-+/*]+ } // FIXME not all of these are special
  126. MathChar { ![0-9^_=<>()\-+/*\\{}\[\]\n$%&~]+ }
  127. @precedence { Number, MathSpecialChar, MathChar }
  128. Ampersand { "&" }
  129. Tilde { "~" }
  130. EnvName { $[a-zA-Z]+ $[*]? }
  131. }
  132. @top LaTeX {
  133. Text
  134. }
  135. @skip { Comment }
  136. // TEXT MODE
  137. optionalWhitespace {
  138. !argument Whitespace
  139. }
  140. OptionalArgument {
  141. !argument OpenBracket ShortOptionalArg CloseBracket
  142. }
  143. TextArgument {
  144. !argument OpenBrace LongArg CloseBrace
  145. }
  146. SectioningArgument {
  147. !argument OpenBrace LongArg CloseBrace
  148. }
  149. LabelArgument {
  150. !argument ShortTextArgument
  151. }
  152. RefArgument {
  153. !argument ShortTextArgument
  154. }
  155. BibKeyArgument {
  156. !argument ShortTextArgument
  157. }
  158. PackageArgument {
  159. !argument ShortTextArgument
  160. }
  161. TabularArgument {
  162. !argument OpenBrace TabularContent CloseBrace
  163. }
  164. UrlArgument {
  165. OpenBrace LiteralArgContent CloseBrace
  166. }
  167. FilePathArgument {
  168. OpenBrace LiteralArgContent CloseBrace
  169. }
  170. BareFilePathArgument {
  171. Whitespace SpaceDelimitedLiteralArgContent
  172. }
  173. DefinitionArgument {
  174. !argument NewLine? Whitespace* OpenBrace DefinitionFragment CloseBrace
  175. }
  176. argument[@isGroup="$Argument"] {
  177. TextArgument
  178. | SectioningArgument
  179. | LabelArgument
  180. | RefArgument
  181. | BibKeyArgument
  182. | PackageArgument
  183. | UrlArgument
  184. | FilePathArgument
  185. | BareFilePathArgument
  186. | DefinitionArgument
  187. }
  188. MacroParameter {
  189. "#" ("1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9")
  190. }
  191. OptionalMacroParameter {
  192. OpenBracket MacroParameter CloseBracket
  193. }
  194. // The autocompletion code in services/web/frontend/js/features/source-editor/utils/tree-operations/commands.ts
  195. // depends on following the `KnownCommand { Command { CommandCtrlSeq [args] } }`
  196. // structure
  197. KnownCommand {
  198. Title {
  199. TitleCtrlSeq optionalWhitespace? OptionalArgument? TextArgument
  200. } |
  201. Author {
  202. AuthorCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? TextArgument
  203. } |
  204. Affil {
  205. AffilCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? TextArgument
  206. } |
  207. Affiliation {
  208. AffiliationCtrlSeq optionalWhitespace? OptionalArgument? optionalWhitespace? TextArgument
  209. } |
  210. DocumentClass {
  211. DocumentClassCtrlSeq optionalWhitespace? OptionalArgument?
  212. DocumentClassArgument { ShortTextArgument }
  213. } |
  214. BibliographyCommand {
  215. BibliographyCtrlSeq optionalWhitespace?
  216. BibliographyArgument { ShortTextArgument }
  217. } |
  218. BibliographyStyleCommand {
  219. BibliographyStyleCtrlSeq optionalWhitespace?
  220. BibliographyStyleArgument { ShortTextArgument }
  221. } |
  222. UsePackage {
  223. UsePackageCtrlSeq optionalWhitespace? OptionalArgument?
  224. PackageArgument
  225. } |
  226. TextColorCommand {
  227. TextColorCtrlSeq optionalWhitespace? ShortTextArgument optionalWhitespace? TextArgument
  228. } |
  229. ColorBoxCommand {
  230. ColorBoxCtrlSeq optionalWhitespace? ShortTextArgument optionalWhitespace? TextArgument
  231. } |
  232. HrefCommand {
  233. HrefCtrlSeq optionalWhitespace? UrlArgument ShortTextArgument
  234. } |
  235. NewTheoremCommand {
  236. NewTheoremCtrlSeq "*"? optionalWhitespace? ShortTextArgument ((OptionalArgument? TextArgument) | (TextArgument OptionalArgument))
  237. } |
  238. TheoremStyleCommand {
  239. TheoremStyleCtrlSeq optionalWhitespace? ShortTextArgument
  240. } |
  241. UrlCommand {
  242. UrlCtrlSeq optionalWhitespace? UrlArgument
  243. } |
  244. VerbCommand {
  245. VerbCtrlSeq VerbContent
  246. } |
  247. LstInlineCommand {
  248. LstInlineCtrlSeq optionalWhitespace? OptionalArgument? LstInlineContent
  249. } |
  250. IncludeGraphics {
  251. IncludeGraphicsCtrlSeq optionalWhitespace? OptionalArgument?
  252. IncludeGraphicsArgument { FilePathArgument }
  253. } |
  254. Caption {
  255. CaptionCtrlSeq "*"? optionalWhitespace? OptionalArgument? TextArgument
  256. } |
  257. Label {
  258. LabelCtrlSeq optionalWhitespace? LabelArgument
  259. } |
  260. Ref {
  261. (RefCtrlSeq | RefStarrableCtrlSeq "*"?) optionalWhitespace? OptionalArgument? optionalWhitespace? OptionalArgument? optionalWhitespace? RefArgument
  262. } |
  263. Cite {
  264. (CiteCtrlSeq | CiteStarrableCtrlSeq "*"?) optionalWhitespace? OptionalArgument? optionalWhitespace? OptionalArgument? optionalWhitespace? BibKeyArgument
  265. } |
  266. Def {
  267. // allow more general Csname argument to \def commands, since other symbols such as '@' are often used in definitions
  268. DefCtrlSeq optionalWhitespace? (Csname | CtrlSym) optionalWhitespace? (MacroParameter | OptionalMacroParameter)* optionalWhitespace? DefinitionArgument
  269. } |
  270. Hbox {
  271. HboxCtrlSeq optionalWhitespace? TextArgument
  272. } |
  273. Left {
  274. LeftCtrlSeq optionalWhitespace?
  275. } |
  276. Right {
  277. RightCtrlSeq optionalWhitespace?
  278. } |
  279. NewCommand {
  280. NewCommandCtrlSeq optionalWhitespace?
  281. (Csname | OpenBrace LiteralArgContent CloseBrace)
  282. (OptionalArgument)*
  283. DefinitionArgument
  284. } |
  285. RenewCommand {
  286. RenewCommandCtrlSeq optionalWhitespace?
  287. (Csname | OpenBrace LiteralArgContent CloseBrace)
  288. (OptionalArgument)*
  289. DefinitionArgument
  290. } |
  291. NewEnvironment {
  292. NewEnvironmentCtrlSeq optionalWhitespace?
  293. (OpenBrace LiteralArgContent CloseBrace)
  294. (OptionalArgument)*
  295. DefinitionArgument
  296. DefinitionArgument
  297. } |
  298. RenewEnvironment {
  299. RenewEnvironmentCtrlSeq optionalWhitespace?
  300. (Csname | OpenBrace LiteralArgContent CloseBrace)
  301. (OptionalArgument)*
  302. DefinitionArgument
  303. DefinitionArgument
  304. } |
  305. Input {
  306. InputCtrlSeq InputArgument { ( FilePathArgument | BareFilePathArgument ) }
  307. } |
  308. Include {
  309. IncludeCtrlSeq IncludeArgument { FilePathArgument }
  310. } |
  311. Centering {
  312. CenteringCtrlSeq
  313. } |
  314. Item {
  315. ItemCtrlSeq optionalWhitespace?
  316. } |
  317. Maketitle {
  318. MaketitleCtrlSeq optionalWhitespace?
  319. } |
  320. HorizontalLine {
  321. (HLineCtrlSeq | TopRuleCtrlSeq | MidRuleCtrlSeq | BottomRuleCtrlSeq) optionalWhitespace?
  322. } |
  323. MultiColumn {
  324. MultiColumnCtrlSeq
  325. optionalWhitespace? SpanArgument { ShortTextArgument }
  326. optionalWhitespace? ColumnArgument { ShortTextArgument }
  327. optionalWhitespace? TabularArgument
  328. }
  329. }
  330. UnknownCommand {
  331. (CtrlSeq !argument Whitespace (OptionalArgument | TextArgument)+)
  332. | ((CtrlSeq | MathTextCtrlSeq) (OptionalArgument | TextArgument)+)
  333. | CtrlSeq Whitespace?
  334. | CtrlSym
  335. }
  336. Command {
  337. KnownCommand
  338. | UnknownCommand
  339. | KnownCtrlSym
  340. }
  341. KnownCtrlSym {
  342. LineBreak {
  343. LineBreakCtrlSym OptionalArgument?
  344. }
  345. }
  346. textBase {
  347. ( Command
  348. | DollarMath
  349. | BracketMath
  350. | ParenMath
  351. | NewLine
  352. | Normal
  353. | Whitespace
  354. | Ampersand
  355. | Tilde
  356. )
  357. }
  358. textWithBrackets {
  359. ( textBase
  360. | OpenBracket
  361. | CloseBracket
  362. )
  363. }
  364. textWithEnvironmentsAndBlankLines {
  365. ( BlankLine
  366. | KnownEnvironment
  367. | Environment
  368. | textWithBrackets
  369. )
  370. }
  371. textWithGroupsEnvironmentsAndBlankLines {
  372. textWithEnvironmentsAndBlankLines
  373. | Group<Text>
  374. }
  375. Content<Element> {
  376. Element
  377. }
  378. SectioningCommand<Command> {
  379. Command optionalWhitespace? "*"? optionalWhitespace? OptionalArgument? optionalWhitespace? SectioningArgument
  380. }
  381. documentSection<Command, Next> {
  382. SectioningCommand<Command> Content<(sectionText | !section Next)*>
  383. }
  384. Book[@isGroup="$SectioningCommand"] { documentSection<BookCtrlSeq, Part | Chapter | Section | SubSection | SubSubSection | Paragraph | SubParagraph> }
  385. Part[@isGroup="$SectioningCommand"] { documentSection<PartCtrlSeq, Chapter | Section | SubSection | SubSubSection | Paragraph | SubParagraph> }
  386. Chapter[@isGroup="$SectioningCommand"] { documentSection<ChapterCtrlSeq, Section | SubSection | SubSubSection | Paragraph | SubParagraph> }
  387. Section[@isGroup="$SectioningCommand"] { documentSection<SectionCtrlSeq, SubSection | SubSubSection | Paragraph | SubParagraph> }
  388. SubSection[@isGroup="$SectioningCommand"] { documentSection<SubSectionCtrlSeq, SubSubSection | Paragraph | SubParagraph> }
  389. SubSubSection[@isGroup="$SectioningCommand"] { documentSection<SubSubSectionCtrlSeq, Paragraph | SubParagraph> }
  390. Paragraph[@isGroup="$SectioningCommand"] { documentSection<ParagraphCtrlSeq, SubParagraph> }
  391. SubParagraph[@isGroup="$SectioningCommand"] { SectioningCommand<SubParagraphCtrlSeq> Content<sectionText*> }
  392. sectioningCommand {
  393. Book | Part | Chapter | Section | SubSection | SubSubSection | Paragraph | SubParagraph
  394. }
  395. sectionText {
  396. !section (
  397. textWithGroupsEnvironmentsAndBlankLines
  398. )+
  399. }
  400. Text {
  401. ( sectionText
  402. | sectioningCommand)+
  403. }
  404. LongArg {
  405. ( textWithBrackets
  406. | NonEmptyGroup<LongArg>
  407. | KnownEnvironment
  408. | Environment
  409. | BlankLine
  410. | "#" // macro character
  411. | "_" | "^" // other math chars
  412. )*
  413. }
  414. ShortTextArgument {
  415. OpenBrace ShortArg CloseBrace
  416. }
  417. ShortArg {
  418. ( textWithBrackets
  419. | NonEmptyGroup<ShortArg>
  420. | "#" // macro character
  421. | "_" | "^" // other math chars
  422. )*
  423. }
  424. ShortOptionalArg {
  425. ( textBase
  426. | NonEmptyGroup<ShortOptionalArg>
  427. | "#" // macro character
  428. )*
  429. }
  430. TikzPictureContent { /// same as Text but with added allowed characters
  431. ( textWithEnvironmentsAndBlankLines
  432. | NonEmptyGroup<TikzPictureContent>
  433. | "#" // macro character
  434. | "_" | "^" // other math chars
  435. )+
  436. }
  437. DefinitionFragment {
  438. ( KnownCommand
  439. | CtrlSeq optionalWhitespace?
  440. | CtrlSym
  441. | Begin
  442. | End
  443. | NonEmptyGroup<DefinitionFragment>
  444. | Dollar
  445. | OpenParenCtrlSym
  446. | CloseParenCtrlSym
  447. | OpenBracketCtrlSym
  448. | CloseBracketCtrlSym
  449. | KnownCtrlSym
  450. | BlankLine
  451. | NewLine
  452. | Normal
  453. | Whitespace
  454. | OpenBracket
  455. | CloseBracket
  456. | "#" // macro character
  457. | Ampersand // for tables
  458. | Tilde // unbreakable space
  459. | "_" | "^" // other math chars
  460. | SectioningCommand<
  461. BookCtrlSeq |
  462. PartCtrlSeq |
  463. ChapterCtrlSeq |
  464. SectionCtrlSeq |
  465. SubSectionCtrlSeq |
  466. SubSubSectionCtrlSeq |
  467. ParagraphCtrlSeq |
  468. SubParagraphCtrlSeq
  469. >
  470. )*
  471. }
  472. KnownEnvironment {
  473. ( DocumentEnvironment
  474. | TabularEnvironment
  475. | EquationEnvironment
  476. | EquationArrayEnvironment
  477. | VerbatimEnvironment
  478. | TikzPictureEnvironment
  479. | FigureEnvironment
  480. | ListEnvironment
  481. | TableEnvironment
  482. )
  483. }
  484. BeginEnv<name> {
  485. Begin
  486. EnvNameGroup<name>
  487. OptionalArgument?
  488. (!argument TextArgument)*
  489. }
  490. EndEnv<name> {
  491. End
  492. EnvNameGroup<name>
  493. }
  494. DocumentEnvironment[@isGroup="$Environment"] {
  495. BeginEnv<DocumentEnvName>
  496. Content<Text>
  497. EndEnv<DocumentEnvName>
  498. (TrailingWhitespaceOnly | TrailingContent)?
  499. }
  500. TabularContent {
  501. (textWithGroupsEnvironmentsAndBlankLines)*
  502. }
  503. TabularEnvironment[@isGroup="$Environment"] {
  504. BeginEnv<TabularEnvName>
  505. Content<TabularContent>
  506. EndEnv<TabularEnvName>
  507. }
  508. TableEnvironment[@isGroup="$Environment"] {
  509. BeginEnv<TableEnvName>
  510. Content<Text>
  511. EndEnv<TableEnvName>
  512. }
  513. EquationEnvironment[@isGroup="$Environment"] {
  514. BeginEnv<EquationEnvName>
  515. Content<Math?>
  516. EndEnv<EquationEnvName>
  517. }
  518. EquationArrayEnvironment[@isGroup="$Environment"] {
  519. BeginEnv<EquationArrayEnvName>
  520. Content<Math?>
  521. EndEnv<EquationArrayEnvName>
  522. }
  523. VerbatimEnvironment[@isGroup="$Environment"] {
  524. BeginEnv<VerbatimEnvName>
  525. Content<VerbatimContent>
  526. EndEnv<VerbatimEnvName>
  527. }
  528. TikzPictureEnvironment[@isGroup="$Environment"] {
  529. BeginEnv<TikzPictureEnvName>
  530. Content<TikzPictureContent>
  531. EndEnv<TikzPictureEnvName>
  532. }
  533. FigureEnvironment[@isGroup="$Environment"] {
  534. BeginEnv<FigureEnvName>
  535. Content<Text>
  536. EndEnv<FigureEnvName>
  537. }
  538. ListEnvironment[@isGroup="$Environment"] {
  539. BeginEnv<ListEnvName>
  540. Content<Text>
  541. EndEnv<ListEnvName>
  542. }
  543. EnvNameGroup<name> {
  544. OpenBrace name CloseBrace
  545. }
  546. Environment[@isGroup="$Environment"] {
  547. BeginEnv<EnvName?>
  548. Content<Text>
  549. EndEnv<EnvName?>
  550. }
  551. Group<GroupContent> {
  552. OpenBrace GroupContent? CloseBrace
  553. }
  554. NonEmptyGroup<GroupContent> {
  555. OpenBrace GroupContent CloseBrace
  556. }
  557. /// MATH MODE
  558. DollarMath {
  559. Dollar (InlineMath | DisplayMath) Dollar
  560. }
  561. InlineMath {
  562. Math
  563. }
  564. DisplayMath {
  565. Dollar Math? Dollar
  566. }
  567. OpenParenMath[closedBy=CloseParenMath] {
  568. OpenParenCtrlSym
  569. }
  570. CloseParenMath[openedBy=OpenParenMath] {
  571. CloseParenCtrlSym
  572. }
  573. // alternative syntax \( math \) for inline math, it is the same as $ math $
  574. ParenMath {
  575. OpenParenMath
  576. Math?
  577. CloseParenMath
  578. }
  579. OpenBracketMath[closedBy=CloseBracketMath] {
  580. OpenBracketCtrlSym
  581. }
  582. CloseBracketMath[openedBy=OpenBracketMath] {
  583. CloseBracketCtrlSym
  584. }
  585. // alternative syntax \[ math \] for display math, it is the same as $$ math $$
  586. BracketMath {
  587. OpenBracketMath
  588. Math?
  589. CloseBracketMath
  590. }
  591. // FIXME: we should have separate math modes for inline and display math,
  592. // because display math can contain blank lines while inline math cannot.
  593. Math {
  594. ( MathTextCommand
  595. | MathCommand
  596. | MathCtrlSym
  597. | MathGroup
  598. | MathDelimitedGroup
  599. | MathSpecialChar
  600. | Number
  601. | NewLine
  602. | KnownEnvironment
  603. | Environment
  604. | MathChar
  605. | OpenBracket
  606. | CloseBracket
  607. | Ampersand
  608. | Tilde
  609. | Label {
  610. LabelCtrlSeq optionalWhitespace? OptionalArgument? LabelArgument
  611. }
  612. | Ref {
  613. (RefCtrlSeq | RefStarrableCtrlSeq "*"?) optionalWhitespace? OptionalArgument? optionalWhitespace? OptionalArgument? optionalWhitespace? RefArgument
  614. }
  615. )+
  616. }
  617. MathTextCommand {
  618. (MathTextCtrlSeq | HboxCtrlSeq) optionalWhitespace? "*"? TextArgument
  619. }
  620. MathCommand { CtrlSeq }
  621. MathCtrlSym { CtrlSym | KnownCtrlSym }
  622. MathGroup {
  623. OpenBrace Math? CloseBrace
  624. }
  625. MathDelimitedGroup {
  626. MathOpening Math? MathClosing
  627. }
  628. // FIXME: we have the same problem with specialize on \left,\right as the delimiters
  629. MathOpening {
  630. LeftCtrlSeq optionalWhitespace? MathDelimiter
  631. }
  632. MathClosing {
  633. RightCtrlSeq optionalWhitespace? MathDelimiter
  634. }
  635. // NOTE: precedence works differently for rules and token, in the rule
  636. // you have to give a specifier !foo which is defined in the @precedence
  637. // block here.
  638. @precedence {
  639. section @left,
  640. argument @left // make CtrlSeq arguments left associative
  641. }