git.iko.soy/typeable/pandoc
on master help sign in
LaTEX reader: properly resolve section numbers with \ref and chapters.

Closes #4529.

commitd77e8f45c9eba5dc5aca579a009a4cd78fbcbc54 authorJohn MacFarlane <jgm@berkeley.edu> dateThu 5 Apr 2018, 10:12 (9 years ago) parent87dda21
2 files changed+43−1
src/Text/Pandoc/Readers/LaTeX.hs+7−1 test/command/4529.md+36
src/Text/Pandoc/Readers/LaTeX.hs+7−1
@@ -164,6 +164,7 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
164164 , sInTableCell :: Bool
165165 , sLastHeaderNum :: HeaderNum
166166 , sLabels :: M.Map String [Inline]
167+ , sHasChapters :: Bool
167168 , sToggles :: M.Map String Bool
168169 }
169170 deriving Show
@@ -183,6 +184,7 @@ defaultLaTeXState = LaTeXState{ sOptions = def
183184 , sInTableCell = False
184185 , sLastHeaderNum = HeaderNum []
185186 , sLabels = M.empty
187+ , sHasChapters = False
186188 , sToggles = M.empty
187189 }
188190
@@ -1984,9 +1986,13 @@ section starred (ident, classes, kvs) lvl = do
19841986 try (spaces >> controlSeq "label"
19851987 >> spaces >> toksToString <$> braced)
19861988 let classes' = if starred then "unnumbered" : classes else classes
1989+ when (lvl == 0) $
1990+ updateState $ \st -> st{ sHasChapters = True }
19871991 unless starred $ do
19881992 hn <- sLastHeaderNum <$> getState
1989− let num = incrementHeaderNum lvl hn
1993+ hasChapters <- sHasChapters <$> getState
1994+ let lvl' = lvl + if hasChapters then 1 else 0
1995+ let num = incrementHeaderNum lvl' hn
19901996 updateState $ \st -> st{ sLastHeaderNum = num }
19911997 updateState $ \st -> st{ sLabels = M.insert lab
19921998 [Str (renderHeaderNum num)]
test/command/4529.mdnew file+36
@@ -0,0 +1,36 @@
1+```
2+% pandoc -f latex -t plain
3+\chapter{First chapter}\label{sec:chp1}
4+The next chapter is Chapter~\ref{sec:chp2}.
5+\section{First section}\label{sec:chp1sec1}
6+The next section is Section~\ref{sec:chp2sec1}.
7+
8+\chapter{Second chapter}\label{sec:chp2}
9+The previous chapter is Chapter~\ref{sec:chp1}.
10+\section{First section}\label{sec:chp2sec1}
11+The previous section is Section~\ref{sec:chp1sec1}.
12+^D
13+
14+
15+FIRST CHAPTER
16+
17+
18+The next chapter is Chapter 2.
19+
20+
21+First section
22+
23+The next section is Section 2.1.
24+
25+
26+
27+SECOND CHAPTER
28+
29+
30+The previous chapter is Chapter 1.
31+
32+
33+First section
34+
35+The previous section is Section 1.1.
36+```