diff options
author | John MacFarlane <jgm@berkeley.edu> | 2020-02-11 22:23:22 -0800 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2020-02-11 22:28:06 -0800 |
commit | 3fbee8c6edb2caf76530f834f0001950b44c5b17 (patch) | |
tree | 62e9ba9071c30f3a7fa122c0e837320f3ec7edc1 | |
parent | 4bd8a35e8ffbf3e17584dc46cb647e0f1ea14b91 (diff) | |
download | pandoc-3fbee8c6edb2caf76530f834f0001950b44c5b17.tar.gz |
LaTeX reader: resolve `\ref` to table numbers.
Closes #6137.
-rw-r--r-- | src/Text/Pandoc/Readers/LaTeX.hs | 66 | ||||
-rw-r--r-- | src/Text/Pandoc/Readers/LaTeX/Parsing.hs | 2 | ||||
-rw-r--r-- | test/command/6137.md | 31 |
3 files changed, 74 insertions, 25 deletions
diff --git a/src/Text/Pandoc/Readers/LaTeX.hs b/src/Text/Pandoc/Readers/LaTeX.hs index 0bafa0d19..cae7a23b7 100644 --- a/src/Text/Pandoc/Readers/LaTeX.hs +++ b/src/Text/Pandoc/Readers/LaTeX.hs @@ -2078,33 +2078,40 @@ addImageCaption = walkM go case attr' of ("", _, _) -> return () (ident, _, _) -> do - st <- getState - let chapnum = - case (sHasChapters st, sLastHeaderNum st) of - (True, DottedNum (n:_)) -> Just n - _ -> Nothing - let num = case sLastFigureNum st of - DottedNum [m,n] -> - case chapnum of - Just m' | m' == m -> DottedNum [m, n+1] - | otherwise -> DottedNum [m', 1] - Nothing -> DottedNum [1] - -- shouldn't happen - DottedNum [n] -> - case chapnum of - Just m -> DottedNum [m, 1] - Nothing -> DottedNum [n + 1] - _ -> - case chapnum of - Just n -> DottedNum [n, 1] - Nothing -> DottedNum [1] - setState $ + num <- getNextNumber sLastFigureNum + updateState $ \st -> st{ sLastFigureNum = num , sLabels = M.insert ident [Str (renderDottedNum num)] (sLabels st) } return $ Image attr' alt' (src, tit') go x = return x +getNextNumber :: Monad m + => (LaTeXState -> DottedNum) -> LP m DottedNum +getNextNumber getCurrentNum = do + st <- getState + let chapnum = + case (sHasChapters st, sLastHeaderNum st) of + (True, DottedNum (n:_)) -> Just n + _ -> Nothing + return $ + case getCurrentNum st of + DottedNum [m,n] -> + case chapnum of + Just m' | m' == m -> DottedNum [m, n+1] + | otherwise -> DottedNum [m', 1] + Nothing -> DottedNum [1] + -- shouldn't happen + DottedNum [n] -> + case chapnum of + Just m -> DottedNum [m, 1] + Nothing -> DottedNum [n + 1] + _ -> + case chapnum of + Just n -> DottedNum [n, 1] + Nothing -> DottedNum [1] + + coloredBlock :: PandocMonad m => Text -> LP m Blocks coloredBlock stylename = try $ do skipopts @@ -2379,10 +2386,19 @@ simpTable envname hasWidthParameter = try $ do addTableCaption :: PandocMonad m => Blocks -> LP m Blocks addTableCaption = walkM go where go (Table c als ws hs rs) = do - (mbcapt, _) <- sCaption <$> getState - return $ case mbcapt of - Just ils -> Table (toList ils) als ws hs rs - Nothing -> Table c als ws hs rs + (mbcapt, mblabel) <- sCaption <$> getState + capt <- case (mbcapt, mblabel) of + (Just ils, Nothing) -> return $ toList ils + (Just ils, Just lab) -> do + num <- getNextNumber sLastTableNum + updateState $ \st -> + st{ sLastTableNum = num + , sLabels = M.insert lab + [Str (renderDottedNum num)] + (sLabels st) } + return $ toList ils -- add number?? + (Nothing, _) -> return c + return $ Table capt als ws hs rs go x = return x diff --git a/src/Text/Pandoc/Readers/LaTeX/Parsing.hs b/src/Text/Pandoc/Readers/LaTeX/Parsing.hs index a6836c3c1..4aa9976c9 100644 --- a/src/Text/Pandoc/Readers/LaTeX/Parsing.hs +++ b/src/Text/Pandoc/Readers/LaTeX/Parsing.hs @@ -120,6 +120,7 @@ data LaTeXState = LaTeXState{ sOptions :: ReaderOptions , sInTableCell :: Bool , sLastHeaderNum :: DottedNum , sLastFigureNum :: DottedNum + , sLastTableNum :: DottedNum , sLabels :: M.Map Text [Inline] , sHasChapters :: Bool , sToggles :: M.Map Text Bool @@ -141,6 +142,7 @@ defaultLaTeXState = LaTeXState{ sOptions = def , sInTableCell = False , sLastHeaderNum = DottedNum [] , sLastFigureNum = DottedNum [] + , sLastTableNum = DottedNum [] , sLabels = M.empty , sHasChapters = False , sToggles = M.empty diff --git a/test/command/6137.md b/test/command/6137.md new file mode 100644 index 000000000..9c5b71d0c --- /dev/null +++ b/test/command/6137.md @@ -0,0 +1,31 @@ +``` +% pandoc -f latex -t native +This reference to Table \ref{tbl:label} doesn't work. +\begin{table} + \caption{This caption has no number.}\label{tbl:label} + \begin{tabular}{ l c r } + -------- & -------- & -------- \\ + -------- & -------- & -------- \\ + \end{tabular} +\end{table} + +This reference to Figure \ref{fig:label} works fine. +\begin{figure} + \includegraphics[width=\textwidth]{example.png} + \caption{A numbered caption, if I use pandoc-crossref.}\label{fig:label} +\end{figure} +^D +[Para [Str "This",Space,Str "reference",Space,Str "to",Space,Str "Table",Space,Link ("",[],[("reference-type","ref"),("reference","tbl:label")]) [Str "1"] ("#tbl:label",""),Space,Str "doesn\8217t",Space,Str "work."] +,Table [Str "This",Space,Str "caption",Space,Str "has",Space,Str "no",Space,Str "number.",Span ("",[],[("label","tbl:label")]) []] [AlignLeft,AlignCenter,AlignRight] [0.0,0.0,0.0] + [[] + ,[] + ,[]] + [[[Plain [Str "\8212\8212\8211"]] + ,[Plain [Str "\8212\8212\8211"]] + ,[Plain [Str "\8212\8212\8211"]]] + ,[[Plain [Str "\8212\8212\8211"]] + ,[Plain [Str "\8212\8212\8211"]] + ,[Plain [Str "\8212\8212\8211"]]]] +,Para [Str "This",Space,Str "reference",Space,Str "to",Space,Str "Figure",Space,Link ("",[],[("reference-type","ref"),("reference","fig:label")]) [Str "1"] ("#fig:label",""),Space,Str "works",Space,Str "fine."] +,Para [Image ("fig:label",[],[("width","\\textwidth")]) [Str "A",Space,Str "numbered",Space,Str "caption,",Space,Str "if",Space,Str "I",Space,Str "use",Space,Str "pandoc-crossref.",Span ("",[],[("label","fig:label")]) []] ("example.png","fig:")]] +``` |