Skip to content

Commit 45c3166

Browse files
committed
LaTeX reader: evaluate theorem name when used...
rather than evaluating it when the `\newtheorem` command is encountered. It may include macros only defined later. Closes #11608.
1 parent 2b5600f commit 45c3166

4 files changed

Lines changed: 68 additions & 9 deletions

File tree

src/Text/Pandoc/Readers/LaTeX.hs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -985,7 +985,7 @@ blockCommands = M.fromList
985985
, ("address", mempty <$ (skipopts *> tok >>= addMeta "address"))
986986
, ("signature", mempty <$ (skipopts *> authors))
987987
, ("date", mempty <$ (skipopts *> tok >>= addMeta "date"))
988-
, ("newtheorem", newtheorem inline)
988+
, ("newtheorem", newtheorem)
989989
, ("theoremstyle", theoremstyle)
990990
-- KOMA-Script metadata commands
991991
, ("extratitle", mempty <$ (skipopts *> tok >>= addMeta "extratitle"))
@@ -1156,7 +1156,7 @@ environment = try $ do
11561156
name <- untokenize <$> braced
11571157
M.findWithDefault mzero name environments <|>
11581158
langEnvironment name <|>
1159-
theoremEnvironment blocks opt name <|>
1159+
theoremEnvironment blocks inlines opt name <|>
11601160
if M.member name (inlineEnvironments
11611161
:: M.Map Text (LP PandocPure Inlines))
11621162
then mzero

src/Text/Pandoc/Readers/LaTeX/Math.hs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ theoremstyle = do
135135
Just sty -> updateState $ \s -> s{ sLastTheoremStyle = sty }
136136
return mempty
137137

138-
newtheorem :: PandocMonad m => LP m Inlines -> LP m Blocks
139-
newtheorem inline = do
138+
newtheorem :: PandocMonad m => LP m Blocks
139+
newtheorem = do
140140
number <- option True (False <$ symbol '*' <* sp)
141141
name <- untokenize <$> braced
142142
sp
143143
series <- option Nothing $ Just . untokenize <$> bracketedToks
144144
sp
145-
showName <- tokWith inline
145+
showName <- braced <|> ((:[]) <$> anyTok)
146146
sp
147147
syncTo <- option Nothing $ Just . untokenize <$> bracketedToks
148148
sty <- sLastTheoremStyle <$> getState
@@ -168,8 +168,12 @@ extractLabelFromBlock (Para inlines) = extractLabel Nothing inlines
168168
extractLabelFromBlock _ = Nothing
169169

170170
theoremEnvironment :: PandocMonad m
171-
=> LP m Blocks -> LP m Inlines -> Text -> LP m Blocks
172-
theoremEnvironment blocks opt name = do
171+
=> LP m Blocks
172+
-> LP m Inlines
173+
-> LP m Inlines
174+
-> Text
175+
-> LP m Blocks
176+
theoremEnvironment blocks inlines opt name = do
173177
resetCaption
174178
tmap <- sTheoremMap <$> getState
175179
case M.lookup name tmap of
@@ -206,7 +210,8 @@ theoremEnvironment blocks opt name = do
206210
PlainStyle -> B.strong
207211
DefinitionStyle -> B.strong
208212
RemarkStyle -> B.emph
209-
let title = titleEmph (theoremName tspec <> number)
213+
tname <- parseFromToks inlines (theoremName tspec)
214+
let title = titleEmph (tname <> number)
210215
<> optTitle <> "." <> space
211216
return $ divWith (fromMaybe "" mblabel, [name], [])
212217
$ addTitle title

src/Text/Pandoc/Readers/LaTeX/Parsing.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ data TheoremStyle =
141141

142142
data TheoremSpec =
143143
TheoremSpec
144-
{ theoremName :: Inlines
144+
{ theoremName :: [Tok]
145145
, theoremStyle :: TheoremStyle
146146
, theoremSeries :: Maybe Text
147147
, theoremSyncTo :: Maybe Text

test/command/11608.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
```
2+
% pandoc -f latex -t plain
3+
\documentclass[english]{article}
4+
\usepackage[T1]{fontenc}
5+
\usepackage[utf8]{inputenc}
6+
\usepackage{amsthm}
7+
\theoremstyle{plain}
8+
\newtheorem{thm}{\protect\theoremname}
9+
\newtheorem{prop}[thm]{\protect\propositionname}
10+
\usepackage{babel}
11+
\providecommand{\propositionname}{Proposition}
12+
\providecommand{\theoremname}{Theorem}
13+
14+
\begin{document}
15+
\begin{thm}
16+
first theorem
17+
\end{thm}
18+
\begin{prop}
19+
first proposition
20+
\end{prop}
21+
22+
\end{document}
23+
<div class="thm">
24+
<p><strong>Theorem 1</strong>. <em>first theorem</em></p>
25+
</div>
26+
<div class="prop">
27+
<p><strong>Proposition 2</strong>. <em>first proposition</em></p>
28+
</div>
29+
jgm@Johns-MacBook-Air:~/src/pandoc % `make binpath` -f latex -t plain
30+
\documentclass[english]{article}
31+
\usepackage[T1]{fontenc}
32+
\usepackage[utf8]{inputenc}
33+
\usepackage{amsthm}
34+
\theoremstyle{plain}
35+
\newtheorem{thm}{\protect\theoremname}
36+
\newtheorem{prop}[thm]{\protect\propositionname}
37+
\usepackage{babel}
38+
\providecommand{\propositionname}{Proposition}
39+
\providecommand{\theoremname}{Theorem}
40+
41+
\begin{document}
42+
\begin{thm}
43+
first theorem
44+
\end{thm}
45+
\begin{prop}
46+
first proposition
47+
\end{prop}
48+
49+
\end{document}
50+
^D
51+
Theorem 1. first theorem
52+
53+
Proposition 2. first proposition
54+
```

0 commit comments

Comments
 (0)