LaTeX reader: Handle \label and \ref for footnotes.

Closes #7930.
This commit is contained in:
John MacFarlane 2022-02-19 11:45:35 -08:00
parent a3117bc142
commit 6fe8014a2c
3 changed files with 117 additions and 1 deletions

View file

@ -383,7 +383,7 @@ inlineCommands = M.unions
, ("MakeTextLowercase", makeLowercase <$> tok)
, ("lowercase", makeLowercase <$> tok)
, ("thanks", skipopts >> note <$> grouped block)
, ("footnote", skipopts >> note <$> grouped block)
, ("footnote", skipopts >> footnote)
, ("passthrough", tok) -- \passthrough macro used by latex writer
-- for listings
, ("includegraphics", do options <- option [] keyvals
@ -431,6 +431,22 @@ today =
text . T.pack . showGregorian . localDay . zonedTimeToLocalTime
<$> getZonedTime
footnote :: PandocMonad m => LP m Inlines
footnote = do
updateState $ \st -> st{ sLastNoteNum = sLastNoteNum st + 1 }
contents <- grouped block >>= walkM resolveNoteLabel
return $ note contents
resolveNoteLabel :: PandocMonad m => Inline -> LP m Inline
resolveNoteLabel (Span (_,cls,kvs) _)
| Just lab <- lookup "label" kvs = do
updateState $ \st -> st{
sLabels = M.insert lab (toList . text . tshow $ sLastNoteNum st)
$ sLabels st }
return $ Span (lab,cls,kvs) []
resolveNoteLabel il = return il
lettrine :: PandocMonad m => LP m Inlines
lettrine = do
optional rawopt

View file

@ -162,6 +162,7 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions
, sLastHeaderNum :: DottedNum
, sLastFigureNum :: DottedNum
, sLastTableNum :: DottedNum
, sLastNoteNum :: Int
, sTheoremMap :: M.Map Text TheoremSpec
, sLastTheoremStyle :: TheoremStyle
, sLastLabel :: Maybe Text
@ -189,6 +190,7 @@ defaultLaTeXState = LaTeXState{ sOptions = def
, sLastHeaderNum = DottedNum []
, sLastFigureNum = DottedNum []
, sLastTableNum = DottedNum []
, sLastNoteNum = 0
, sTheoremMap = M.empty
, sLastTheoremStyle = PlainStyle
, sLastLabel = Nothing

98
test/command/7930.md Normal file
View file

@ -0,0 +1,98 @@
```
% pandoc -f latex -t native
We discuss foobar in notes \ref{note:X} and \ref{note:Y}.
Foo.\footnote{\label{note:X}A note. See also note~\ref{note:Y}.}
Bar.\footnote{\label{note:Y}Another note. See also
note~\ref{note:X}}
^D
[ Para
[ Str "We"
, Space
, Str "discuss"
, Space
, Str "foobar"
, Space
, Str "in"
, Space
, Str "notes"
, Space
, Link
( ""
, []
, [ ( "reference-type" , "ref" )
, ( "reference" , "note:X" )
]
)
[ Str "1" ]
( "#note:X" , "" )
, Space
, Str "and"
, Space
, Link
( ""
, []
, [ ( "reference-type" , "ref" )
, ( "reference" , "note:Y" )
]
)
[ Str "2" ]
( "#note:Y" , "" )
, Str "."
]
, Para
[ Str "Foo."
, Note
[ Para
[ Span ( "note:X" , [] , [ ( "label" , "note:X" ) ] ) []
, Str "A"
, Space
, Str "note."
, Space
, Str "See"
, Space
, Str "also"
, Space
, Str "note\160"
, Link
( ""
, []
, [ ( "reference-type" , "ref" )
, ( "reference" , "note:Y" )
]
)
[ Str "2" ]
( "#note:Y" , "" )
, Str "."
]
]
]
, Para
[ Str "Bar."
, Note
[ Para
[ Span ( "note:Y" , [] , [ ( "label" , "note:Y" ) ] ) []
, Str "Another"
, Space
, Str "note."
, Space
, Str "See"
, Space
, Str "also"
, SoftBreak
, Str "note\160"
, Link
( ""
, []
, [ ( "reference-type" , "ref" )
, ( "reference" , "note:X" )
]
)
[ Str "1" ]
( "#note:X" , "" )
]
]
]
]
```