    {-# LANGUAGE OverloadedStrings   #-}
    {-# LANGUAGE LambdaCase          #-}
    {-# LANGUAGE TupleSections       #-}
    {-# LANGUAGE PatternGuards       #-}
    {-# LANGUAGE ScopedTypeVariables #-}
    {-# LANGUAGE TypeApplications    #-}
    {-# LANGUAGE ViewPatterns        #-}
    {- |
       Module      : Text.Pandoc.Writers.LaTeX
       Copyright   : Copyright (C) 2006-2025 John MacFarlane
       License     : GNU GPL, version 2 or above

       Maintainer  : John MacFarlane <jgm@berkeley.edu>
       Stability   : alpha
       Portability : portable

    Conversion of 'Pandoc' format into LaTeX.
    -}
    module Text.Pandoc.Writers.LaTeX (
        writeLaTeX
      , writeBeamer
      ) where
    import Control.Monad.State.Strict
        ( MonadState(get, put),
          gets,
          modify,
          evalStateT )
    import Control.Monad
        ( MonadPlus(mplus),
          liftM,
          when,
          unless )
    import Crypto.Hash (hashWith, MD5(MD5))
    import Data.Containers.ListUtils (nubOrd)
    import Data.Char (isDigit, isAscii, isLetter)
    import Data.List (intersperse, partition, (\\))
    import qualified Data.Set as Set
    import Data.Maybe (catMaybes, fromMaybe, isJust, listToMaybe, mapMaybe, isNothing)
    import Data.Monoid (Any (..))
    import Data.Text (Text)
    import qualified Data.Text as T
    import Network.URI (unEscapeString)
    import Text.DocTemplates (Context(..), FromContext(lookupContext), Val(..), renderTemplate)
    import qualified Data.Map as M
    import Text.Collate.Lang (renderLang)
    import Text.Pandoc.Class.PandocMonad (PandocMonad, getPOSIXTime, lookupEnv,
                                          report, toLang)
    import Text.Pandoc.Definition
    import Text.Pandoc.Highlighting (formatLaTeXBlock, formatLaTeXInline, highlight,
                                     defaultStyle, styleToLaTeX)
    import Text.Pandoc.ImageSize
    import Text.Pandoc.Logging
    import Text.Pandoc.Options
    import Text.DocLayout
    import Text.Pandoc.Shared
    import Text.Pandoc.URI
    import Text.Pandoc.Slides
    import Text.Pandoc.Walk (query, walk, walkM)
    import Text.Pandoc.Writers.LaTeX.Caption (getCaption)
    import Text.Pandoc.Writers.LaTeX.Table (tableToLaTeX)
    import Text.Pandoc.Writers.LaTeX.Citation (citationsToNatbib,
                                               citationsToBiblatex)
    import Text.Pandoc.Writers.LaTeX.Types (LW, WriterState (..), startingState,
                                            PdfStandard (..))
    import Text.Pandoc.Writers.LaTeX.Lang (toBabel)
    import Text.Pandoc.Writers.LaTeX.Util (stringToLaTeX, StringContext(..),
                                           toLabel, inCmd,
                                           wrapDiv, hypertarget, labelFor,
                                           getListingsLanguage, mbBraced)
    import Text.Pandoc.Writers.Shared
    import qualified Data.Attoparsec.Text as A
    import qualified Text.Pandoc.UTF8 as UTF8
    import qualified Text.Pandoc.Writers.AnnotatedTable as Ann
    import Control.Applicative ((<|>))

    -- Work around problems with notes inside emphasis (see #8982)
    isolateBigNotes :: ([Inline] -> Inline) -> [Inline] -> [Inline]
    isolateBigNotes :: ([Inline] -> Inline) -> [Inline] -> [Inline]
    isolateBigNotes [Inline] -> Inline
    constructor [Inline]
    xs =
      let ([Inline]
    before, [Inline]
    after) = (Inline -> Bool) -> [Inline] -> ([Inline], [Inline])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    break Inline -> Bool
    isBigNote [Inline]
    xs
      in case [Inline]
    after of
           (Inline
    noteInline:[Inline]
    rest) -> [Inline] -> Inline
    constructor [Inline]
    before Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    :
                                Inline
    noteInline Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    :
                                ([Inline] -> Inline) -> [Inline] -> [Inline]
    isolateBigNotes [Inline] -> Inline
    constructor [Inline]
    rest
           [] -> [[Inline] -> Inline
    constructor [Inline]
    xs]

    isBigNote :: Inline -> Bool
    isBigNote :: Inline -> Bool
    isBigNote (Note [Plain [Inline]
    _]) = Bool
    False  -- A small note
    isBigNote (Note [Para [Inline]
    _]) =  Bool
    False  -- A small note
    isBigNote (Note [Block]
    _) = Bool
    True  -- A big note
    isBigNote Inline
    _ = Bool
    False  -- Not a note

    raiseBigNotes :: [Inline] -> [Inline]
    raiseBigNotes :: [Inline] -> [Inline]
    raiseBigNotes (Emph [Inline]
    inner : [Inline]
    xs)
      = ([Inline] -> Inline) -> [Inline] -> [Inline]
    isolateBigNotes [Inline] -> Inline
    Emph ([Inline] -> [Inline]
    raiseBigNotes [Inline]
    inner) [Inline] -> [Inline] -> [Inline]
    forall a. [a] -> [a] -> [a]
    ++ [Inline] -> [Inline]
    raiseBigNotes [Inline]
    xs
    raiseBigNotes (Strong [Inline]
    inner : [Inline]
    xs)
      = ([Inline] -> Inline) -> [Inline] -> [Inline]
    isolateBigNotes [Inline] -> Inline
    Strong ([Inline] -> [Inline]
    raiseBigNotes [Inline]
    inner) [Inline] -> [Inline] -> [Inline]
    forall a. [a] -> [a] -> [a]
    ++ [Inline] -> [Inline]
    raiseBigNotes [Inline]
    xs
    raiseBigNotes (Underline [Inline]
    inner : [Inline]
    xs)
      = ([Inline] -> Inline) -> [Inline] -> [Inline]
    isolateBigNotes [Inline] -> Inline
    Underline ([Inline] -> [Inline]
    raiseBigNotes [Inline]
    inner) [Inline] -> [Inline] -> [Inline]
    forall a. [a] -> [a] -> [a]
    ++ [Inline] -> [Inline]
    raiseBigNotes [Inline]
    xs
    raiseBigNotes (Strikeout [Inline]
    inner : [Inline]
    xs)
      = ([Inline] -> Inline) -> [Inline] -> [Inline]
    isolateBigNotes [Inline] -> Inline
    Strikeout ([Inline] -> [Inline]
    raiseBigNotes [Inline]
    inner) [Inline] -> [Inline] -> [Inline]
    forall a. [a] -> [a] -> [a]
    ++ [Inline] -> [Inline]
    raiseBigNotes [Inline]
    xs
    raiseBigNotes (Inline
    x : [Inline]
    xs)          = Inline
    x Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    raiseBigNotes [Inline]
    xs
    raiseBigNotes [] = []

    -- | Convert Pandoc to LaTeX.
    writeLaTeX :: PandocMonad m => WriterOptions -> Pandoc -> m Text
    writeLaTeX :: forall (m :: * -> *).
    PandocMonad m =>
    WriterOptions -> Pandoc -> m Text
    writeLaTeX WriterOptions
    options Pandoc
    document = do
      let Any Bool
    hasBigNotes =
           (Inline -> Any) -> Pandoc -> Any
    forall c. Monoid c => (Inline -> c) -> Pandoc -> c
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (\Inline
    il -> if Inline -> Bool
    isBigNote Inline
    il then Bool -> Any
    Any Bool
    True else Bool -> Any
    Any Bool
    False) Pandoc
    document
      let document' :: Pandoc
    document' = if Bool
    hasBigNotes
                         then ([Inline] -> [Inline]) -> Pandoc -> Pandoc
    forall a b. Walkable a b => (a -> a) -> b -> b
    walk [Inline] -> [Inline]
    raiseBigNotes Pandoc
    document
                         else Pandoc
    document
      StateT WriterState m Text -> WriterState -> m Text
    forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
    evalStateT (WriterOptions -> Pandoc -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    WriterOptions -> Pandoc -> LW m Text
    pandocToLaTeX WriterOptions
    options Pandoc
    document') (WriterState -> m Text) -> WriterState -> m Text
    forall a b. (a -> b) -> a -> b
    $ WriterOptions -> WriterState
    startingState WriterOptions
    options

    -- | Convert Pandoc to LaTeX Beamer.
    writeBeamer :: PandocMonad m => WriterOptions -> Pandoc -> m Text
    writeBeamer :: forall (m :: * -> *).
    PandocMonad m =>
    WriterOptions -> Pandoc -> m Text
    writeBeamer WriterOptions
    options Pandoc
    document =
      StateT WriterState m Text -> WriterState -> m Text
    forall (m :: * -> *) s a. Monad m => StateT s m a -> s -> m a
    evalStateT (WriterOptions -> Pandoc -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    WriterOptions -> Pandoc -> LW m Text
    pandocToLaTeX WriterOptions
    options Pandoc
    document) (WriterState -> m Text) -> WriterState -> m Text
    forall a b. (a -> b) -> a -> b
    $
        (WriterOptions -> WriterState
    startingState WriterOptions
    options){ stBeamer = True }

    pandocToLaTeX :: PandocMonad m
                  => WriterOptions -> Pandoc -> LW m Text
    pandocToLaTeX :: forall (m :: * -> *).
    PandocMonad m =>
    WriterOptions -> Pandoc -> LW m Text
    pandocToLaTeX WriterOptions
    options (Pandoc Meta
    meta [Block]
    blocks) = do
      -- Strip off 'references' header if --natbib or --biblatex
      let method :: CiteMethod
    method = WriterOptions -> CiteMethod
    writerCiteMethod WriterOptions
    options
      let isRefsDiv :: Block -> Bool
    isRefsDiv (Div (Text
    "refs",[Text]
    _,[(Text, Text)]
    _) [Block]
    _) = Bool
    True
          isRefsDiv Block
    _ = Bool
    False
      let blocks' :: [Block]
    blocks' = if CiteMethod
    method CiteMethod -> CiteMethod -> Bool
    forall a. Eq a => a -> a -> Bool
    == CiteMethod
    Biblatex Bool -> Bool -> Bool
    || CiteMethod
    method CiteMethod -> CiteMethod -> Bool
    forall a. Eq a => a -> a -> Bool
    == CiteMethod
    Natbib
                       then (Block -> Bool) -> [Block] -> [Block]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (Bool -> Bool
    not (Bool -> Bool) -> (Block -> Bool) -> Block -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Block -> Bool
    isRefsDiv) [Block]
    blocks
                       else [Block]
    blocks
      -- see if there are internal links
      let isInternalLink :: Inline -> [Text]
    isInternalLink (Link (Text, [Text], [(Text, Text)])
    _ [Inline]
    _ (Text
    s,Text
    _))
            | Just (Char
    '#', Text
    xs) <- Text -> Maybe (Char, Text)
    T.uncons Text
    s = [Text
    xs]
          isInternalLink Inline
    _                 = []
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    s -> WriterState
    s{ stInternalLinks = query isInternalLink blocks' }
      let colwidth :: Maybe Int
    colwidth = if WriterOptions -> WrapOption
    writerWrapText WriterOptions
    options WrapOption -> WrapOption -> Bool
    forall a. Eq a => a -> a -> Bool
    == WrapOption
    WrapAuto
                        then Int -> Maybe Int
    forall a. a -> Maybe a
    Just (Int -> Maybe Int) -> Int -> Maybe Int
    forall a b. (a -> b) -> a -> b
    $ WriterOptions -> Int
    writerColumns WriterOptions
    options
                        else Maybe Int
    forall a. Maybe a
    Nothing
      docLangs <- [Maybe Lang] -> [Lang]
    forall a. [Maybe a] -> [a]
    catMaybes ([Maybe Lang] -> [Lang])
    -> StateT WriterState m [Maybe Lang] -> StateT WriterState m [Lang]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
          (Text -> StateT WriterState m (Maybe Lang))
    -> [Text] -> StateT WriterState m [Maybe Lang]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
    mapM (Maybe Text -> StateT WriterState m (Maybe Lang)
    forall (m :: * -> *). PandocMonad m => Maybe Text -> m (Maybe Lang)
    toLang (Maybe Text -> StateT WriterState m (Maybe Lang))
    -> (Text -> Maybe Text)
    -> Text
    -> StateT WriterState m (Maybe Lang)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Maybe Text
    forall a. a -> Maybe a
    Just) ([Text] -> [Text]
    forall a. Ord a => [a] -> [a]
    nubOrd ((Block -> [Text]) -> [Block] -> [Text]
    forall c. Monoid c => (Block -> c) -> [Block] -> c
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (Text -> Block -> [Text]
    extract Text
    "lang") [Block]
    blocks))
      mblang <- toLang $ case getLang options meta of
                              Just Text
    l -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    l
                              Maybe Text
    Nothing | [Lang] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Lang]
    docLangs -> Maybe Text
    forall a. Maybe a
    Nothing
                                      | Bool
    otherwise     -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just Text
    "en"
      modify $ \WriterState
    s -> WriterState
    s{ stLang = mblang }
      metadata <- metaToContext options
                  blockListToLaTeX
                  (fmap chomp . inlineListToLaTeX)
                  meta
      let chaptersClasses = [Text
    "memoir",Text
    "book",Text
    "report",Text
    "scrreprt",Text
    "scrreport",
                            Text
    "scrbook",Text
    "extreport",Text
    "extbook",Text
    "tufte-book",
                            Text
    "ctexrep",Text
    "ctexbook",Text
    "elegantbook"]
      let frontmatterClasses = [Text
    "memoir",Text
    "book",Text
    "scrbook",Text
    "extbook",Text
    "tufte-book",
                               Text
    "ctexbook",Text
    "elegantbook"]
      -- these have \frontmatter etc.
      beamer <- gets stBeamer
      let documentClass =
            case Text -> Context Text -> Maybe Text
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    lookupContext Text
    "documentclass" (WriterOptions -> Context Text
    writerVariables WriterOptions
    options) Maybe Text -> Maybe Text -> Maybe Text
    forall a. Maybe a -> Maybe a -> Maybe a
    forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
    `mplus`
                  (MetaValue -> Text
    forall a. Walkable Inline a => a -> Text
    stringify (MetaValue -> Text) -> Maybe MetaValue -> Maybe Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> Meta -> Maybe MetaValue
    lookupMeta Text
    "documentclass" Meta
    meta) of
                     Just Text
    x -> Text
    x
                     Maybe Text
    Nothing | Bool
    beamer    -> Text
    "beamer"
                             | Bool
    otherwise -> case WriterOptions -> TopLevelDivision
    writerTopLevelDivision WriterOptions
    options of
                                              TopLevelDivision
    TopLevelPart    -> Text
    "book"
                                              TopLevelDivision
    TopLevelChapter -> Text
    "book"
                                              TopLevelDivision
    _               -> Text
    "article"
      when (documentClass `elem` chaptersClasses) $
         modify $ \WriterState
    s -> WriterState
    s{ stHasChapters = True }
      let csquotes =
            case Text -> Context Text -> Maybe (Val Text)
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    lookupContext Text
    "csquotes" (WriterOptions -> Context Text
    writerVariables WriterOptions
    options) of
              Just (BoolVal Bool
    v) -> Bool
    v
              Just (SimpleVal (Text Int
    _ Text
    t)) -> Text
    t Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    /= (Text
    "false" :: Text)
              Maybe (Val Text)
    _ -> case MetaValue -> Text
    forall a. Walkable Inline a => a -> Text
    stringify (MetaValue -> Text) -> Maybe MetaValue -> Maybe Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> Meta -> Maybe MetaValue
    lookupMeta Text
    "csquotes" Meta
    meta of
                      Maybe Text
    Nothing -> Bool
    False
                      Just Text
    "false" -> Bool
    False
                      Just Text
    _ -> Bool
    True
      when csquotes $ modify $ \WriterState
    s -> WriterState
    s{stCsquotes = True}
      let (blocks'', lastHeader) = if writerCiteMethod options == Citeproc then
                                     (blocks', [])
                                   else case reverse blocks' of
                                     Header Int
    1 (Text, [Text], [(Text, Text)])
    _ [Inline]
    il : [Block]
    _ -> ([Block] -> [Block]
    forall a. HasCallStack => [a] -> [a]
    init [Block]
    blocks', [Inline]
    il)
                                     [Block]
    _                 -> ([Block]
    blocks', [])
      blocks''' <- if beamer
                      then toSlides blocks''
                      else return $ makeSections False Nothing blocks''
      main <- blockListToLaTeX blocks'''
      biblioTitle <- inlineListToLaTeX lastHeader
      st <- get
      titleMeta <- escapeCommas <$> -- see #10501
                    stringToLaTeX TextString (stringify $ docTitle meta)
      subtitleMeta <- stringToLaTeX TextString (stringify $ lookupMetaInlines "subtitle" meta)
      authorsMeta <- mapM (stringToLaTeX TextString . stringify) $ docAuthors meta
      -- The trailer ID is as hash used to identify the PDF. Taking control of its
      -- value is important when aiming for reproducible PDF generation. Setting
      -- `SOURCE_DATE_EPOCH` is the traditional method used to control
      -- reproducible builds. There are no cryptographic requirements for the ID,
      -- so the 128bits (16 bytes) of MD5 are appropriate.
      reproduciblePDF <- isJust <$> lookupEnv "SOURCE_DATE_EPOCH"
      trailerID <- do
        time <- getPOSIXTime
        let hash = String -> Text
    T.pack (String -> Text) -> (ByteString -> String) -> ByteString -> Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Digest MD5 -> String
    forall a. Show a => a -> String
    show (Digest MD5 -> String)
    -> (ByteString -> Digest MD5) -> ByteString -> String
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . MD5 -> ByteString -> Digest MD5
    forall ba alg.
    (ByteArrayAccess ba, HashAlgorithm alg) =>
    alg -> ba -> Digest alg
    hashWith MD5
    MD5 (ByteString -> Text) -> ByteString -> Text
    forall a b. (a -> b) -> a -> b
    $ [ByteString] -> ByteString
    forall a. Monoid a => [a] -> a
    mconcat
                   [ String -> ByteString
    UTF8.fromString (String -> ByteString) -> String -> ByteString
    forall a b. (a -> b) -> a -> b
    $ POSIXTime -> String
    forall a. Show a => a -> String
    show POSIXTime
    time
                   , Text -> ByteString
    UTF8.fromText (Text -> ByteString) -> Text -> ByteString
    forall a b. (a -> b) -> a -> b
    $ Maybe Int -> Doc Text -> Text
    forall a. HasChars a => Maybe Int -> Doc a -> a
    render Maybe Int
    forall a. Maybe a
    Nothing Doc Text
    main
                   ]
        pure $ mconcat [ "<", hash, "> <", hash, ">" ]
      -- we need a default here since lang is used in template conditionals
      let hasStringValue Text
    x = Maybe (Doc Text) -> Bool
    forall a. Maybe a -> Bool
    isJust (Text -> Context Text -> Maybe (Doc Text)
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    getField Text
    x Context Text
    metadata :: Maybe (Doc Text))
      let geometryFromMargins = [Doc Text] -> Doc Text
    forall a. Monoid a => [a] -> a
    mconcat ([Doc Text] -> Doc Text) -> [Doc Text] -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Doc Text -> [Doc Text] -> [Doc Text]
    forall a. a -> [a] -> [a]
    intersperse (Doc Text
    "," :: Doc Text) ([Doc Text] -> [Doc Text]) -> [Doc Text] -> [Doc Text]
    forall a b. (a -> b) -> a -> b
    $
                                ((Doc Text, Text) -> Maybe (Doc Text))
    -> [(Doc Text, Text)] -> [Doc Text]
    forall a b. (a -> Maybe b) -> [a] -> [b]
    mapMaybe (\(Doc Text
    x,Text
    y) ->
                                    ((Doc Text
    x Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    "=") Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>) (Doc Text -> Doc Text) -> Maybe (Doc Text) -> Maybe (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> Context Text -> Maybe (Doc Text)
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    getField Text
    y Context Text
    metadata)
                                  [(Doc Text
    "lmargin",Text
    "margin-left")
                                  ,(Doc Text
    "rmargin",Text
    "margin-right")
                                  ,(Doc Text
    "tmargin",Text
    "margin-top")
                                  ,(Doc Text
    "bmargin",Text
    "margin-bottom")
                                  ]

      let dirs = (Block -> [Text]) -> [Block] -> [Text]
    forall c. Monoid c => (Block -> c) -> [Block] -> c
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (Text -> Block -> [Text]
    extract Text
    "dir") [Block]
    blocks

      let nociteIds = (Inline -> [Text]) -> [Inline] -> [Text]
    forall c. Monoid c => (Inline -> c) -> [Inline] -> c
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (\case
                               Cite [Citation]
    cs [Inline]
    _ -> (Citation -> Text) -> [Citation] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Citation -> Text
    citationId [Citation]
    cs
                               Inline
    _         -> [])
                        ([Inline] -> [Text]) -> [Inline] -> [Text]
    forall a b. (a -> b) -> a -> b
    $ Text -> Meta -> [Inline]
    lookupMetaInlines Text
    "nocite" Meta
    meta

       -- see #7414, avoid escaped underscores
      let unescapeUnderscore = HasCallStack => Text -> Text -> Text -> Text
    Text -> Text -> Text -> Text
    T.replace Text
    "\\_" Text
    "_"
      let bibliography' = (Text -> Text) -> [Text] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Text -> Text
    unescapeUnderscore ([Text] -> [Text]) -> Maybe [Text] -> Maybe [Text]
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
                            Text -> Context Text -> Maybe [Text]
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    getField Text
    "bibliography" Context Text
    metadata

      -- Process PDF standard metadata for DocumentMetadata
      pdfStd <- processPdfStandard metadata

      let context  =  (case Maybe [Text]
    bibliography' of
                         Maybe [Text]
    Nothing -> Context Text -> Context Text
    forall a. a -> a
    id
                         Just [Text]
    xs -> Text -> [Text] -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    resetField Text
    "bibliography" [Text]
    xs) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "toc" (WriterOptions -> Bool
    writerTableOfContents WriterOptions
    options) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "lof" (WriterOptions -> Bool
    writerListOfFigures WriterOptions
    options) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "lot" (WriterOptions -> Bool
    writerListOfTables WriterOptions
    options) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "toc-depth" (Int -> Text
    forall a. Show a => a -> Text
    tshow
                                            (WriterOptions -> Int
    writerTOCDepth WriterOptions
    options Int -> Int -> Int
    forall a. Num a => a -> a -> a
    -
                                                  if WriterState -> Bool
    stHasChapters WriterState
    st
                                                     then Int
    1
                                                     else Int
    0)) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "body" Doc Text
    main (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "title-meta" Text
    titleMeta (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "subtitle-meta" Text
    subtitleMeta (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "author-meta"
                            (Text -> [Text] -> Text
    T.intercalate Text
    "; " [Text]
    authorsMeta) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "documentclass" Text
    documentClass (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "verbatim-in-note" (WriterState -> Bool
    stVerbInNote WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "tables" (WriterState -> Bool
    stTable WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "multirow" (WriterState -> Bool
    stMultiRow WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "cancel" (WriterState -> Bool
    stCancel WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "strikeout" (WriterState -> Bool
    stStrikeout WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "url" (WriterState -> Bool
    stUrl WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "numbersections" (WriterOptions -> Bool
    writerNumberSections WriterOptions
    options) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "lhs" (WriterState -> Bool
    stLHS WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "graphics" (WriterState -> Bool
    stGraphics WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "subfigure" (WriterState -> Bool
    stSubfigure WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "svg" (WriterState -> Bool
    stSVG WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "has-chapters" (WriterState -> Bool
    stHasChapters WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "has-frontmatter" (Text
    documentClass Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    frontmatterClasses) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "listings" (WriterOptions -> HighlightMethod
    writerHighlightMethod WriterOptions
    options HighlightMethod -> HighlightMethod -> Bool
    forall a. Eq a => a -> a -> Bool
    ==
                                           HighlightMethod
    IdiomaticHighlighting
                                           Bool -> Bool -> Bool
    || WriterState -> Bool
    stLHS WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "zero-width-non-joiner" (WriterState -> Bool
    stZwnj WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "beamer" Bool
    beamer (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      (if WriterState -> Bool
    stHighlighting WriterState
    st
                          then case WriterOptions -> HighlightMethod
    writerHighlightMethod WriterOptions
    options of
                                 Skylighting Style
    sty ->
                                       Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "highlighting-macros"
                                          (Text -> Text
    T.stripEnd (Text -> Text) -> Text -> Text
    forall a b. (a -> b) -> a -> b
    $ Style -> Text
    styleToLaTeX Style
    sty)
                                 HighlightMethod
    DefaultHighlighting ->
                                       Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "highlighting-macros"
                                          (Text -> Text
    T.stripEnd (Text -> Text) -> Text -> Text
    forall a b. (a -> b) -> a -> b
    $ Style -> Text
    styleToLaTeX Style
    defaultStyle)
                                 HighlightMethod
    _ -> Context Text -> Context Text
    forall a. a -> a
    id
                          else Context Text -> Context Text
    forall a. a -> a
    id) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      (case WriterOptions -> CiteMethod
    writerCiteMethod WriterOptions
    options of
                             CiteMethod
    Natbib   -> Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "biblio-title" Doc Text
    biblioTitle (Context Text -> Context Text)
    -> (Context Text -> Context Text) -> Context Text -> Context Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                                         Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "natbib" Bool
    True (Context Text -> Context Text)
    -> (Context Text -> Context Text) -> Context Text -> Context Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                                         Text -> [Text] -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "nocite-ids" [Text]
    nociteIds
                             CiteMethod
    Biblatex -> Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "biblio-title" Doc Text
    biblioTitle (Context Text -> Context Text)
    -> (Context Text -> Context Text) -> Context Text -> Context Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                                         Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "biblatex" Bool
    True (Context Text -> Context Text)
    -> (Context Text -> Context Text) -> Context Text -> Context Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                                         Text -> [Text] -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "nocite-ids" [Text]
    nociteIds
                             CiteMethod
    _        -> Context Text -> Context Text
    forall a. a -> a
    id) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "colorlinks" ((Text -> Bool) -> [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any Text -> Bool
    hasStringValue
                               [Text
    "citecolor", Text
    "urlcolor", Text
    "linkcolor", Text
    "toccolor",
                                Text
    "filecolor"]) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      (if [Text] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    dirs
                         then Context Text -> Context Text
    forall a. a -> a
    id
                         else Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "dir" (Text
    "ltr" :: Text)) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "section-titles" Bool
    True (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "csl-refs" (WriterState -> Bool
    stHasCslRefs WriterState
    st) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "geometry" Doc Text
    geometryFromMargins (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      (case Text -> Maybe (Char, Text)
    T.uncons (Text -> Maybe (Char, Text))
    -> (Doc Text -> Text) -> Doc Text -> Maybe (Char, Text)
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Maybe Int -> Doc Text -> Text
    forall a. HasChars a => Maybe Int -> Doc a -> a
    render Maybe Int
    forall a. Maybe a
    Nothing (Doc Text -> Maybe (Char, Text))
    -> Maybe (Doc Text) -> Maybe (Maybe (Char, Text))
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
                            Text -> Context Text -> Maybe (Doc Text)
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    getField Text
    "papersize" Context Text
    metadata of
                          -- uppercase a4, a5, etc.
                          Just (Just (Char
    'A', Text
    ds))
                            | Bool -> Bool
    not (Text -> Bool
    T.null Text
    ds) Bool -> Bool -> Bool
    && (Char -> Bool) -> Text -> Bool
    T.all Char -> Bool
    isDigit Text
    ds
                              -> Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    resetField Text
    "papersize" (Text
    "a" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    ds)
                          Maybe (Maybe (Char, Text))
    _   -> Context Text -> Context Text
    forall a. a -> a
    id) (Context Text -> Context Text)
    -> (Context Text -> Context Text) -> Context Text -> Context Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                      (if Bool
    reproduciblePDF
                        then Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "pdf-trailer-id" Text
    trailerID
                        else Context Text -> Context Text
    forall a. a -> a
    id) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      (if Bool -> Bool
    not ([Text] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null (PdfStandard -> [Text]
    pdfStandards PdfStandard
    pdfStd)) Bool -> Bool -> Bool
    || Maybe Text -> Bool
    forall a. Maybe a -> Bool
    isJust (PdfStandard -> Maybe Text
    pdfVersion PdfStandard
    pdfStd)
                        then Text -> Val Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    resetField Text
    "pdfstandard" (Val Text -> Context Text -> Context Text)
    -> Val Text -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ Context Text -> Val Text
    forall a. Context a -> Val a
    MapVal (Context Text -> Val Text) -> Context Text -> Val Text
    forall a b. (a -> b) -> a -> b
    $ Map Text (Val Text) -> Context Text
    forall a. Map Text (Val a) -> Context a
    Context (Map Text (Val Text) -> Context Text)
    -> Map Text (Val Text) -> Context Text
    forall a b. (a -> b) -> a -> b
    $ [(Text, Val Text)] -> Map Text (Val Text)
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList
                               [ (Text
    "standards", [Val Text] -> Val Text
    forall a. [Val a] -> Val a
    ListVal ([Val Text] -> Val Text) -> [Val Text] -> Val Text
    forall a b. (a -> b) -> a -> b
    $ (Text -> Val Text) -> [Text] -> [Val Text]
    forall a b. (a -> b) -> [a] -> [b]
    map (Doc Text -> Val Text
    forall a. Doc a -> Val a
    SimpleVal (Doc Text -> Val Text) -> (Text -> Doc Text) -> Text -> Val Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal) (PdfStandard -> [Text]
    pdfStandards PdfStandard
    pdfStd))
                               , (Text
    "version", Val Text -> (Text -> Val Text) -> Maybe Text -> Val Text
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Val Text
    forall a. Val a
    NullVal (Doc Text -> Val Text
    forall a. Doc a -> Val a
    SimpleVal (Doc Text -> Val Text) -> (Text -> Doc Text) -> Text -> Val Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal) (PdfStandard -> Maybe Text
    pdfVersion PdfStandard
    pdfStd))
                               , (Text
    "tagging", Bool -> Val Text
    forall a. Bool -> Val a
    BoolVal (PdfStandard -> Bool
    pdfTagging PdfStandard
    pdfStd))
                               ]
                        else Context Text -> Context Text
    forall a. a -> a
    id) (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $
                      Context Text
    metadata
      let babelLang = Maybe Lang
    mblang Maybe Lang -> (Lang -> Maybe Text) -> Maybe Text
    forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Lang -> Maybe Text
    toBabel
      let context' =
              -- note: lang is used in some conditionals in the template,
              -- so we need to set it if we have any babel/polyglossia:
              (Context Text -> Context Text)
    -> (Lang -> Context Text -> Context Text)
    -> Maybe Lang
    -> Context Text
    -> Context Text
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Context Text -> Context Text
    forall a. a -> a
    id (\Lang
    l -> Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "lang"
                          (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Lang -> Text
    renderLang Lang
    l)) Maybe Lang
    mblang
            (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ (Context Text -> Context Text)
    -> (Text -> Context Text -> Context Text)
    -> Maybe Text
    -> Context Text
    -> Context Text
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe Context Text -> Context Text
    forall a. a -> a
    id (\Text
    l -> Text -> Doc Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "babel-lang"
                          (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    l)) Maybe Text
    babelLang
            (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ (case Maybe Text
    babelLang of -- see #8283
                    Just Text
    l | Text
    l Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `notElem` [Text]
    ldfLanguages
                             -> Text -> Text -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "babeloptions" (Text
    "provide=*" :: Text)
                    Maybe Text
    _ -> Context Text -> Context Text
    forall a. a -> a
    id)
            (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ Text -> [Doc Text] -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "babel-otherlangs"
                 ((Text -> Doc Text) -> [Text] -> [Doc Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal
                   ((Text -> Bool) -> [Text] -> [Text]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    ldfLanguages) ([Text] -> [Text])
    -> ([Maybe Text] -> [Text]) -> [Maybe Text] -> [Text]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                    [Text] -> [Text]
    forall a. Ord a => [a] -> [a]
    nubOrd ([Text] -> [Text])
    -> ([Maybe Text] -> [Text]) -> [Maybe Text] -> [Text]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Maybe Text] -> [Text]
    forall a. [Maybe a] -> [a]
    catMaybes ([Maybe Text] -> [Text])
    -> ([Maybe Text] -> [Maybe Text]) -> [Maybe Text] -> [Text]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    .
                    (Maybe Text -> Bool) -> [Maybe Text] -> [Maybe Text]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (Maybe Text -> Maybe Text -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Maybe Text
    babelLang)
                    ([Maybe Text] -> [Text]) -> [Maybe Text] -> [Text]
    forall a b. (a -> b) -> a -> b
    $ (Lang -> Maybe Text) -> [Lang] -> [Maybe Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Lang -> Maybe Text
    toBabel [Lang]
    docLangs))
            (Context Text -> Context Text) -> Context Text -> Context Text
    forall a b. (a -> b) -> a -> b
    $ Text -> Bool -> Context Text -> Context Text
    forall a b. ToContext a b => Text -> b -> Context a -> Context a
    defField Text
    "latex-dir-rtl"
               ((Maybe Int -> Doc Text -> Text
    forall a. HasChars a => Maybe Int -> Doc a -> a
    render Maybe Int
    forall a. Maybe a
    Nothing (Doc Text -> Text) -> Maybe (Doc Text) -> Maybe Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> Context Text -> Maybe (Doc Text)
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    getField Text
    "dir" Context Text
    context) Maybe Text -> Maybe Text -> Bool
    forall a. Eq a => a -> a -> Bool
    ==
                   Text -> Maybe Text
    forall a. a -> Maybe a
    Just (Text
    "rtl" :: Text)) Context Text
    context
      return $ render colwidth $
        case writerTemplate options of
           Maybe (Template Text)
    Nothing  -> Doc Text
    main
           Just Template Text
    tpl -> Template Text -> Context Text -> Doc Text
    forall a b.
    (TemplateTarget a, ToContext a b) =>
    Template a -> b -> Doc a
    renderTemplate Template Text
    tpl Context Text
    context'

    -- Commas in title-meta need to be put in braces; see #10501
    escapeCommas :: Text -> Text
    escapeCommas :: Text -> Text
    escapeCommas = HasCallStack => Text -> Text -> Text -> Text
    Text -> Text -> Text -> Text
    T.replace Text
    "," Text
    "{,}"

    toSlides :: PandocMonad m => [Block] -> LW m [Block]
    toSlides :: forall (m :: * -> *). PandocMonad m => [Block] -> LW m [Block]
    toSlides [Block]
    bs = do
      opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      let slideLevel = Int -> Maybe Int -> Int
    forall a. a -> Maybe a -> a
    fromMaybe ([Block] -> Int
    getSlideLevel [Block]
    bs) (Maybe Int -> Int) -> Maybe Int -> Int
    forall a b. (a -> b) -> a -> b
    $ WriterOptions -> Maybe Int
    writerSlideLevel WriterOptions
    opts
      let bs' = Int -> [Block] -> [Block]
    prepSlides Int
    slideLevel [Block]
    bs
      walkM (elementToBeamer slideLevel) $ makeSections False Nothing bs'

    -- this creates section slides and marks slides with class "slide","block"
    elementToBeamer :: PandocMonad m => Int -> Block -> LW m Block
    elementToBeamer :: forall (m :: * -> *). PandocMonad m => Int -> Block -> LW m Block
    elementToBeamer Int
    slideLevel (Div (Text
    ident,Text
    "section":[Text]
    dclasses,[(Text, Text)]
    dkvs)
                                  xs :: [Block]
    xs@(h :: Block
    h@(Header Int
    lvl (Text, [Text], [(Text, Text)])
    _ [Inline]
    _) : [Block]
    ys))
      | Int
    lvl Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    >  Int
    slideLevel
        = Block -> StateT WriterState m Block
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Block -> StateT WriterState m Block)
    -> Block -> StateT WriterState m Block
    forall a b. (a -> b) -> a -> b
    $ (Text, [Text], [(Text, Text)]) -> [Block] -> Block
    Div (Text
    ident,Text
    "block"Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    dclasses,[(Text, Text)]
    dkvs) [Block]
    xs
      | Int
    lvl Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    <  Int
    slideLevel
        = do let isSlide :: Block -> Bool
    isSlide (Div (Text
    _,Text
    "slide":[Text]
    _,[(Text, Text)]
    _) [Block]
    _)   = Bool
    True
                 isSlide (Div (Text
    _,Text
    "section":[Text]
    _,[(Text, Text)]
    _) [Block]
    _) = Bool
    True
                 isSlide Block
    _                         = Bool
    False
             let ([Block]
    titleBs, [Block]
    slideBs) = (Block -> Bool) -> [Block] -> ([Block], [Block])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    break Block -> Bool
    isSlide [Block]
    ys
             Block -> StateT WriterState m Block
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Block -> StateT WriterState m Block)
    -> Block -> StateT WriterState m Block
    forall a b. (a -> b) -> a -> b
    $
               case [Block]
    titleBs of
                  [] -> (Text, [Text], [(Text, Text)]) -> [Block] -> Block
    Div (Text
    ident,Text
    "section"Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    dclasses,[(Text, Text)]
    dkvs) [Block]
    xs
                  [Div (Text
    _,Text
    "notes":[Text]
    _,[(Text, Text)]
    _) [Block]
    _] ->  -- see #7857, don't create frame
                        -- just for speaker notes after section heading
                        (Text, [Text], [(Text, Text)]) -> [Block] -> Block
    Div (Text
    ident,Text
    "section"Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    dclasses,[(Text, Text)]
    dkvs) [Block]
    xs
                  [Block]
    _  -> (Text, [Text], [(Text, Text)]) -> [Block] -> Block
    Div (Text
    ident,Text
    "section"Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    dclasses,[(Text, Text)]
    dkvs)
                         (Block
    h Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : (Text, [Text], [(Text, Text)]) -> [Block] -> Block
    Div (Text
    "",Text
    "slide"Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    dclasses,[(Text, Text)]
    dkvs) (Block
    hBlock -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    :[Block]
    titleBs) Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : [Block]
    slideBs)
      | Bool
    otherwise
        = Block -> StateT WriterState m Block
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Block -> StateT WriterState m Block)
    -> Block -> StateT WriterState m Block
    forall a b. (a -> b) -> a -> b
    $ (Text, [Text], [(Text, Text)]) -> [Block] -> Block
    Div (Text
    ident,Text
    "slide"Text -> [Text] -> [Text]
    forall a. a -> [a] -> [a]
    :[Text]
    dclasses,[(Text, Text)]
    dkvs) [Block]
    xs
    elementToBeamer Int
    _ Block
    x = Block -> StateT WriterState m Block
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Block
    x

    isListBlock :: Block -> Bool
    isListBlock :: Block -> Bool
    isListBlock (BulletList [[Block]]
    _)     = Bool
    True
    isListBlock (OrderedList ListAttributes
    _ [[Block]]
    _)  = Bool
    True
    isListBlock (DefinitionList [([Inline], [[Block]])]
    _) = Bool
    True
    isListBlock Block
    _                  = Bool
    False

    -- | Convert Pandoc block element to LaTeX.
    blockToLaTeX :: PandocMonad m
                 => Block     -- ^ Block to convert
                 -> LW m (Doc Text)
    blockToLaTeX :: forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX (Div attr :: (Text, [Text], [(Text, Text)])
    attr@(Text
    identifier,Text
    "block":[Text]
    dclasses,[(Text, Text)]
    _)
                 (Header Int
    _ (Text, [Text], [(Text, Text)])
    _ [Inline]
    ils : [Block]
    bs)) = do
      let blockname :: Doc Text
    blockname
            | Text
    "example" Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    dclasses = Doc Text
    "exampleblock"
            | Text
    "alert" Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    dclasses = Doc Text
    "alertblock"
            | Bool
    otherwise = Doc Text
    "block"
      anchor <- if Text -> Bool
    T.null Text
    identifier
                   then Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Doc Text
    forall a. Doc a
    empty
                   else (Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>) (Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> Text -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Text -> LW m (Doc Text)
    hypertarget Text
    identifier
      title' <- inlineListToLaTeX ils
      contents <- blockListToLaTeX bs
      wrapDiv attr $ ("\\begin" <> braces blockname <> braces title' <> anchor) $$
                     contents $$ "\\end" <> braces blockname
    blockToLaTeX (Div (Text
    identifier,Text
    "slide":[Text]
    dclasses,[(Text, Text)]
    dkvs)
                   (Header Int
    _ (Text
    _,[Text]
    hclasses,[(Text, Text)]
    hkvs) [Inline]
    ils : [Block]
    bs)) = do
      -- note: [fragile] is required or verbatim breaks
      let hasCodeBlock :: Block -> [Bool]
    hasCodeBlock (CodeBlock (Text, [Text], [(Text, Text)])
    _ Text
    _) = [Bool
    True]
          hasCodeBlock Block
    _               = []
      let hasCode :: Inline -> [Bool]
    hasCode (Code (Text, [Text], [(Text, Text)])
    _ Text
    _) = [Bool
    True]
          hasCode Inline
    _          = []
      let classes :: [Text]
    classes = [Text] -> [Text]
    forall a. Ord a => [a] -> [a]
    nubOrd ([Text] -> [Text]) -> [Text] -> [Text]
    forall a b. (a -> b) -> a -> b
    $ [Text]
    dclasses [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++ [Text]
    hclasses
      let kvs :: [(Text, Text)]
    kvs = [(Text, Text)] -> [(Text, Text)]
    forall a. Ord a => [a] -> [a]
    nubOrd ([(Text, Text)] -> [(Text, Text)])
    -> [(Text, Text)] -> [(Text, Text)]
    forall a b. (a -> b) -> a -> b
    $ [(Text, Text)]
    dkvs [(Text, Text)] -> [(Text, Text)] -> [(Text, Text)]
    forall a. [a] -> [a] -> [a]
    ++ [(Text, Text)]
    hkvs
      let fragile :: Bool
    fragile = Text
    "fragile" Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes Bool -> Bool -> Bool
    ||
                    Bool -> Bool
    not ([Bool] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null ([Bool] -> Bool) -> [Bool] -> Bool
    forall a b. (a -> b) -> a -> b
    $ (Block -> [Bool]) -> [Block] -> [Bool]
    forall c. Monoid c => (Block -> c) -> [Block] -> c
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query Block -> [Bool]
    hasCodeBlock [Block]
    bs [Bool] -> [Bool] -> [Bool]
    forall a. [a] -> [a] -> [a]
    ++ (Inline -> [Bool]) -> [Block] -> [Bool]
    forall c. Monoid c => (Inline -> c) -> [Block] -> c
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query Inline -> [Bool]
    hasCode [Block]
    bs)
      let frameoptions :: [Text]
    frameoptions = [Text
    "allowdisplaybreaks", Text
    "allowframebreaks", Text
    "fragile",
                          Text
    "b", Text
    "c", Text
    "t", Text
    "environment", Text
    "s", Text
    "squeeze",
                          Text
    "label", Text
    "plain", Text
    "shrink", Text
    "standout",
                          Text
    "noframenumbering", Text
    "containsverbatim"]
      let optionslist :: [Text]
    optionslist = [Text
    "fragile" | Bool
    fragile
                                   , Maybe Text -> Bool
    forall a. Maybe a -> Bool
    isNothing (Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup Text
    "fragile" [(Text, Text)]
    kvs)
                                   , Text
    "fragile" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `notElem` [Text]
    classes
                                   , Text
    "containsverbatim" Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `notElem` [Text]
    classes] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                        [Text
    k | Text
    k <- [Text]
    classes, Text
    k Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    frameoptions] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                        [Text
    k Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    "=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    v | (Text
    k,Text
    v) <- [(Text, Text)]
    kvs, Text
    k Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    frameoptions] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                        [Text
    v | (Text
    "frameoptions", Text
    v) <- [(Text, Text)]
    kvs]
      let options :: Doc Text
    options = if [Text] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    optionslist
                       then Doc Text
    forall a. Doc a
    empty
                       else Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> [Text] -> Text
    T.intercalate Text
    "," [Text]
    optionslist))
      slideTitle <- if [Inline]
    ils [Inline] -> [Inline] -> Bool
    forall a. Eq a => a -> a -> Bool
    == [Text -> Inline
    Str Text
    "\0"] -- marker for hrule
                       then Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
                       else Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    ils
      slideAnchor <- if T.null identifier
                        then pure empty
                        else (cr <>) <$> hypertarget identifier
      contents <- blockListToLaTeX bs >>= wrapDiv (identifier,classes,kvs)
      return $ ("\\begin{frame}" <> options <> slideTitle <> slideAnchor) $$
                 contents $$ "\\end{frame}"
    blockToLaTeX (Div (identifier :: Text
    identifier@(Text -> Maybe (Char, Text)
    T.uncons -> Just (Char
    _,Text
    _)),[Text]
    dclasses,[(Text, Text)]
    dkvs)
                   (Header Int
    lvl (Text
    "",[Text]
    hclasses,[(Text, Text)]
    hkvs) [Inline]
    ils : [Block]
    bs)) =
      -- move identifier from div to header
      Block -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX ((Text, [Text], [(Text, Text)]) -> [Block] -> Block
    Div (Text
    "",[Text]
    dclasses,[(Text, Text)]
    dkvs)
                   (Int -> (Text, [Text], [(Text, Text)]) -> [Inline] -> Block
    Header Int
    lvl (Text
    identifier,[Text]
    hclasses,[(Text, Text)]
    hkvs) [Inline]
    ils Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : [Block]
    bs))
    blockToLaTeX (Div (Text
    identifier,[Text]
    classes,[(Text, Text)]
    kvs) [Block]
    bs) = do
      beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      oldIncremental <- gets stIncremental
      if beamer && "incremental" `elem` classes
         then modify $ \WriterState
    st -> WriterState
    st{ stIncremental = True }
         else when (beamer && "nonincremental" `elem` classes) $
                 modify $ \WriterState
    st -> WriterState
    st { stIncremental = False }
      result <- if (identifier == "refs" || -- <- for backwards compatibility
                    "csl-bib-body" `elem` classes) &&
                   (not (null bs))
                   then do
                     modify $ \WriterState
    st -> WriterState
    st{ stHasCslRefs = True }
                     inner <- blockListToLaTeX bs
                     return $ ("\\begin{CSLReferences}"
                                <> braces
                                    (if "hanging-indent" `elem` classes
                                        then "1"
                                        else "0")
                                <> braces
                                   (maybe "1" literal (lookup "entry-spacing" kvs)))
                              $$ inner
                              $+$ "\\end{CSLReferences}"
                   else blockListToLaTeX bs
      modify $ \WriterState
    st -> WriterState
    st{ stIncremental = oldIncremental }
      let wrap Doc Text
    txt
           | Bool
    beamer Bool -> Bool -> Bool
    && Text
    "notes" Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
             = Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Doc Text
    "\\note" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    txt) -- speaker notes
           | Text
    "ref-" Text -> Text -> Bool
    `T.isPrefixOf` Text
    identifier
             = do
                 lab <- Text -> LW m Text
    forall (m :: * -> *). PandocMonad m => Text -> LW m Text
    toLabel Text
    identifier
                 pure $ ("\\bibitem" <> brackets "\\citeproctext"
                          <> braces (literal lab)) $$ txt
             | Bool
    otherwise = do
                 linkAnchor <- Text -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Text -> LW m (Doc Text)
    hypertarget Text
    identifier
                 pure $ linkAnchor $$ txt
      wrapDiv (identifier,classes,kvs) result >>= wrap
    blockToLaTeX (Plain [Inline]
    lst) =
      [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    -- . . . indicates pause in beamer slides
    blockToLaTeX (Para [Str Text
    ".",Inline
    Space,Str Text
    ".",Inline
    Space,Str Text
    "."]) = do
      beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      if beamer
         then blockToLaTeX (RawBlock "latex" "\\pause")
         else inlineListToLaTeX [Str ".",Space,Str ".",Space,Str "."]
    blockToLaTeX (Para [Inline]
    lst) =
      if [Inline] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Inline]
    lst
         then do
           opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
           if isEnabled Ext_empty_paragraphs opts
              then pure "\\hfill\\par"
              else pure mempty
         else [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    blockToLaTeX (LineBlock [[Inline]]
    lns) =
      Block -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX (Block -> StateT WriterState m (Doc Text))
    -> Block -> StateT WriterState m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ [[Inline]] -> Block
    linesToPara [[Inline]]
    lns
    blockToLaTeX (BlockQuote [Block]
    lst) = do
      beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      csquotes <- liftM stCsquotes get
      case lst of
           [Block
    b] | Bool
    beamer Bool -> Bool -> Bool
    && Block -> Bool
    isListBlock Block
    b -> do
             oldIncremental <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stIncremental
             modify $ \WriterState
    s -> WriterState
    s{ stIncremental = not oldIncremental }
             result <- blockToLaTeX b
             modify $ \WriterState
    s -> WriterState
    s{ stIncremental = oldIncremental }
             return result
           [Block]
    _ -> do
             oldInQuote <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInQuote
             modify (\WriterState
    s -> WriterState
    s{stInQuote = True})
             contents <- blockListToLaTeX lst
             modify (\WriterState
    s -> WriterState
    s{stInQuote = oldInQuote})
             let envname = if Bool
    csquotes then Doc Text
    "displayquote" else Doc Text
    "quote"
             return $ ("\\begin" <> braces envname) $$
                      contents $$
                      ("\\end" <> braces envname)
    blockToLaTeX (CodeBlock (Text
    identifier,[Text]
    classes,[(Text, Text)]
    keyvalAttr) Text
    str) = do
      opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      inNote <- stInNote <$> get
      linkAnchor <- if T.null identifier
                       then pure empty
                       else ((<> cr) . (<> "%")) <$> hypertarget identifier
      let lhsCodeBlock = do
            (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    s -> WriterState
    s{ stLHS = True }
            Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> StateT WriterState m (Doc Text))
    -> Doc Text -> StateT WriterState m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Doc Text -> Doc Text
    forall a. Doc a -> Doc a
    flush (Doc Text
    linkAnchor Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    "\\begin{code}" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    str Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                                Doc Text
    "\\end{code}") Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    forall a. Doc a
    cr
      let rawCodeBlock = do
            env <- if Bool
    inNote
                      then (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\WriterState
    s -> WriterState
    s{ stVerbInNote = True }) StateT WriterState m ()
    -> StateT WriterState m Text -> StateT WriterState m Text
    forall a b.
    StateT WriterState m a
    -> StateT WriterState m b -> StateT WriterState m b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >>
                           Text -> StateT WriterState m Text
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Text
    "Verbatim"
                      else Text -> StateT WriterState m Text
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Text
    "verbatim"
            return $ flush (linkAnchor $$ literal ("\\begin{" <> env <> "}") $$
                     literal str $$ literal ("\\end{" <> env <> "}")) <> cr
      let listingsCodeBlock = do
            st <- StateT WriterState m WriterState
    forall s (m :: * -> *). MonadState s m => m s
    get
            ref <- toLabel identifier
            kvs <- mapM (\(Text
    k,Text
    v) -> (Text
    k,) (Text -> (Text, Text))
    -> StateT WriterState m Text -> StateT WriterState m (Text, Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
                           StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    TextString Text
    v) keyvalAttr
            let params = if WriterOptions -> HighlightMethod
    writerHighlightMethod (WriterState -> WriterOptions
    stOptions WriterState
    st)
                            HighlightMethod -> HighlightMethod -> Bool
    forall a. Eq a => a -> a -> Bool
    == HighlightMethod
    IdiomaticHighlighting
                         then (case [Text] -> Maybe Text
    getListingsLanguage [Text]
    classes of
                                    Just Text
    l  -> [ Text
    "language=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    mbBraced Text
    l ]
                                    Maybe Text
    Nothing -> []) [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                              [ Text
    "numbers=left" | Text
    "numberLines" Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
                                 Bool -> Bool -> Bool
    || Text
    "number" Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
                                 Bool -> Bool -> Bool
    || Text
    "number-lines" Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes ] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                              [ (if Text
    key Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    "startFrom"
                                    then Text
    "firstnumber"
                                    else Text
    key) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    "=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    mbBraced Text
    attr |
                                    (Text
    key,Text
    attr) <- [(Text, Text)]
    kvs,
                                    Text
    key Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `notElem` [Text
    "exports", Text
    "tangle", Text
    "results"]
                                    -- see #4889
                              ] [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++
                              [Text
    "label=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    ref | Bool -> Bool
    not (Text -> Bool
    T.null Text
    identifier)]

                         else []
                printParams
                    | [Text] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    params = Doc Text
    forall a. Doc a
    empty
                    | Bool
    otherwise   = Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    hcat (Doc Text -> [Doc Text] -> [Doc Text]
    forall a. a -> [a] -> [a]
    intersperse Doc Text
    ", "
                          ((Text -> Doc Text) -> [Text] -> [Doc Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal [Text]
    params))
            return $ flush ("\\begin{lstlisting}" <> printParams $$ literal str $$
                     "\\end{lstlisting}") $$ cr
      let highlightedCodeBlock =
            case SyntaxMap
    -> (FormatOptions -> [SourceLine] -> Text)
    -> (Text, [Text], [(Text, Text)])
    -> Text
    -> Either Text Text
    forall a.
    SyntaxMap
    -> (FormatOptions -> [SourceLine] -> a)
    -> (Text, [Text], [(Text, Text)])
    -> Text
    -> Either Text a
    highlight (WriterOptions -> SyntaxMap
    writerSyntaxMap WriterOptions
    opts)
                     FormatOptions -> [SourceLine] -> Text
    formatLaTeXBlock (Text
    "",[Text]
    classes [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++ [Text
    "default"],[(Text, Text)]
    keyvalAttr) Text
    str of
                   Left Text
    msg -> do
                     Bool -> StateT WriterState m () -> StateT WriterState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    unless (Text -> Bool
    T.null Text
    msg) (StateT WriterState m () -> StateT WriterState m ())
    -> StateT WriterState m () -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $
                       LogMessage -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (LogMessage -> StateT WriterState m ())
    -> LogMessage -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ Text -> LogMessage
    CouldNotHighlight Text
    msg
                     StateT WriterState m (Doc Text)
    rawCodeBlock
                   Right Text
    h -> do
                      Bool -> StateT WriterState m () -> StateT WriterState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when Bool
    inNote (StateT WriterState m () -> StateT WriterState m ())
    -> StateT WriterState m () -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\WriterState
    s -> WriterState
    s{ stVerbInNote = True })
                      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\WriterState
    s -> WriterState
    s{ stHighlighting = True })
                      Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (Doc Text -> Doc Text
    forall a. Doc a -> Doc a
    flush (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Doc Text
    linkAnchor Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    h)
      case () of
         ()
    _ | Extension -> WriterOptions -> Bool
    forall a. HasSyntaxExtensions a => Extension -> a -> Bool
    isEnabled Extension
    Ext_literate_haskell WriterOptions
    opts Bool -> Bool -> Bool
    && Text
    "haskell" Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes Bool -> Bool -> Bool
    &&
             Text
    "literate" Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes           -> StateT WriterState m (Doc Text)
    lhsCodeBlock
           | WriterOptions -> HighlightMethod
    writerHighlightMethod WriterOptions
    opts HighlightMethod -> HighlightMethod -> Bool
    forall a. Eq a => a -> a -> Bool
    == HighlightMethod
    IdiomaticHighlighting
                                                 -> StateT WriterState m (Doc Text)
    listingsCodeBlock
           | Bool -> Bool
    not ([Text] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    classes), Skylighting Style
    _ <- WriterOptions -> HighlightMethod
    writerHighlightMethod WriterOptions
    opts
                                                 -> StateT WriterState m (Doc Text)
    highlightedCodeBlock
           | Bool -> Bool
    not ([Text] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    classes), HighlightMethod
    DefaultHighlighting <- WriterOptions -> HighlightMethod
    writerHighlightMethod WriterOptions
    opts
                                                 -> StateT WriterState m (Doc Text)
    highlightedCodeBlock
           -- we don't want to use \begin{verbatim} if our code
           -- contains \end{verbatim}:
           | Bool
    inNote
           , Text
    "\\end{Verbatim}" Text -> Text -> Bool
    `T.isInfixOf` Text
    str -> StateT WriterState m (Doc Text)
    highlightedCodeBlock
           | Bool -> Bool
    not Bool
    inNote
           , Text
    "\\end{verbatim}" Text -> Text -> Bool
    `T.isInfixOf` Text
    str -> StateT WriterState m (Doc Text)
    highlightedCodeBlock
           | Bool
    otherwise                           -> StateT WriterState m (Doc Text)
    rawCodeBlock
    blockToLaTeX b :: Block
    b@(RawBlock Format
    f Text
    x) = do
      beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      if f == Format "latex" || f == Format "tex" ||
           (f == Format "beamer" && beamer)
         then return $ literal x
         else do
           report $ BlockNotRendered b
           return empty
    blockToLaTeX (BulletList []) = Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty  -- otherwise latex error
    blockToLaTeX (BulletList [[Block]]
    lst) = do
      incremental <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stIncremental
      isFirstInDefinition <- gets stIsFirstInDefinition
      beamer <- gets stBeamer
      let inc = if Bool
    beamer Bool -> Bool -> Bool
    && Bool
    incremental then String
    "[<+->]" else String
    ""
      items <- mapM (listItemToLaTeX False) lst
      let spacing = if [[Block]] -> Bool
    isTightList [[Block]]
    lst
                       then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "\\tightlist"
                       else Doc Text
    forall a. Doc a
    empty
      return $ -- force list to start on new line if in a defn list
                 (if isFirstInDefinition then "\\hfill" else mempty) $$
                 text ("\\begin{itemize}" <> inc) $$
                 spacing $$
                 -- force list at beginning of definition to start on new line
                 vcat items $$
                 "\\end{itemize}"
    blockToLaTeX (OrderedList ListAttributes
    _ []) = Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty -- otherwise latex error
    blockToLaTeX (OrderedList (Int
    start, ListNumberStyle
    numstyle, ListNumberDelim
    numdelim) [[Block]]
    lst) = do
      st <- StateT WriterState m WriterState
    forall s (m :: * -> *). MonadState s m => m s
    get
      let inc = if WriterState -> Bool
    stBeamer WriterState
    st Bool -> Bool -> Bool
    && WriterState -> Bool
    stIncremental WriterState
    st then String
    "[<+->]" else String
    ""
      let oldlevel = WriterState -> Int
    stOLLevel WriterState
    st
      isFirstInDefinition <- gets stIsFirstInDefinition
      put $ st {stOLLevel = oldlevel + 1}
      items <- mapM (listItemToLaTeX True) lst
      modify (\WriterState
    s -> WriterState
    s {stOLLevel = oldlevel})
      let beamer = WriterState -> Bool
    stBeamer WriterState
    st
      let tostyle Doc a
    x = case ListNumberStyle
    numstyle of
                           ListNumberStyle
    Decimal      -> Doc a
    "\\arabic" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           ListNumberStyle
    UpperRoman   -> Doc a
    "\\Roman" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           ListNumberStyle
    LowerRoman   -> Doc a
    "\\roman" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           ListNumberStyle
    UpperAlpha   -> Doc a
    "\\Alph" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           ListNumberStyle
    LowerAlpha   -> Doc a
    "\\alph" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           ListNumberStyle
    Example      -> Doc a
    "\\arabic" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
                           ListNumberStyle
    DefaultStyle -> Doc a
    "\\arabic" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x
      let todelim Doc a
    x = case ListNumberDelim
    numdelim of
                           ListNumberDelim
    OneParen  -> Doc a
    x Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a
    ")"
                           ListNumberDelim
    TwoParens -> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    parens Doc a
    x
                           ListNumberDelim
    Period    -> Doc a
    x Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a
    "."
                           ListNumberDelim
    _         -> Doc a
    x Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a
    "."
      let exemplar = case ListNumberStyle
    numstyle of
                           ListNumberStyle
    Decimal      -> Doc Text
    "1"
                           ListNumberStyle
    UpperRoman   -> Doc Text
    "I"
                           ListNumberStyle
    LowerRoman   -> Doc Text
    "i"
                           ListNumberStyle
    UpperAlpha   -> Doc Text
    "A"
                           ListNumberStyle
    LowerAlpha   -> Doc Text
    "a"
                           ListNumberStyle
    Example      -> Doc Text
    "1"
                           ListNumberStyle
    DefaultStyle -> Doc Text
    "1"
      let enum = Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Text
    "enum" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    T.toLower (Int -> Text
    toRomanNumeral Int
    oldlevel)
      let stylecommand
            | ListNumberStyle
    numstyle ListNumberStyle -> ListNumberStyle -> Bool
    forall a. Eq a => a -> a -> Bool
    == ListNumberStyle
    DefaultStyle Bool -> Bool -> Bool
    && ListNumberDelim
    numdelim ListNumberDelim -> ListNumberDelim -> Bool
    forall a. Eq a => a -> a -> Bool
    == ListNumberDelim
    DefaultDelim = Doc Text
    forall a. Doc a
    empty
            | Bool
    beamer Bool -> Bool -> Bool
    && ListNumberStyle
    numstyle ListNumberStyle -> ListNumberStyle -> Bool
    forall a. Eq a => a -> a -> Bool
    == ListNumberStyle
    Decimal Bool -> Bool -> Bool
    && ListNumberDelim
    numdelim ListNumberDelim -> ListNumberDelim -> Bool
    forall a. Eq a => a -> a -> Bool
    == ListNumberDelim
    Period = Doc Text
    forall a. Doc a
    empty
            | Bool
    beamer = Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets (Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    todelim Doc Text
    exemplar)
            | Bool
    otherwise = Doc Text
    "\\def" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    "\\label" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    enum Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
              Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    todelim (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    tostyle Doc Text
    enum)
      let resetcounter = if Int
    start Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == Int
    1 Bool -> Bool -> Bool
    || Int
    oldlevel Int -> Int -> Bool
    forall a. Ord a => a -> a -> Bool
    > Int
    4
                            then Doc Text
    forall a. Doc a
    empty
                            else Doc Text
    "\\setcounter" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    enum Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                                  Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (String -> Doc Text) -> String -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ Int -> String
    forall a. Show a => a -> String
    show (Int -> String) -> Int -> String
    forall a b. (a -> b) -> a -> b
    $ Int
    start Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1)
      let spacing = if [[Block]] -> Bool
    isTightList [[Block]]
    lst
                       then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "\\tightlist"
                       else Doc Text
    forall a. Doc a
    empty
      return $ -- force list at beginning of definition to start on new line
               (if isFirstInDefinition then "\\hfill" else mempty)
             $$ text ("\\begin{enumerate}" <> inc)
             $$ stylecommand
             $$ resetcounter
             $$ spacing
             $$ vcat items
             $$ "\\end{enumerate}"
    blockToLaTeX (DefinitionList []) = Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    blockToLaTeX (DefinitionList [([Inline], [[Block]])]
    lst) = do
      incremental <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stIncremental
      beamer <- gets stBeamer
      let inc = if Bool
    beamer Bool -> Bool -> Bool
    && Bool
    incremental then String
    "[<+->]" else String
    ""
      items <- mapM defListItemToLaTeX lst
      let spacing = if (([Inline], [[Block]]) -> Bool) -> [([Inline], [[Block]])] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    all ([[Block]] -> Bool
    isTightList ([[Block]] -> Bool)
    -> (([Inline], [[Block]]) -> [[Block]])
    -> ([Inline], [[Block]])
    -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . ([Inline], [[Block]]) -> [[Block]]
    forall a b. (a, b) -> b
    snd) [([Inline], [[Block]])]
    lst
                       then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "\\tightlist"
                       else Doc Text
    forall a. Doc a
    empty
      return $ text ("\\begin{description}" <> inc) $$ spacing $$ vcat items $$
                   "\\end{description}"
    blockToLaTeX Block
    HorizontalRule =
                Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return
      Doc Text
    "\\begin{center}\\rule{0.5\\linewidth}{0.5pt}\\end{center}"
    blockToLaTeX (Header Int
    level (Text
    id',[Text]
    classes,[(Text, Text)]
    _) [Inline]
    lst) = do
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    s -> WriterState
    s{stInHeading = True}
      hdr <- [Text]
    -> Text -> Int -> [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    [Text] -> Text -> Int -> [Inline] -> LW m (Doc Text)
    sectionHeader [Text]
    classes Text
    id' Int
    level [Inline]
    lst
      modify $ \WriterState
    s -> WriterState
    s{stInHeading = False}
      return hdr
    blockToLaTeX (Table (Text, [Text], [(Text, Text)])
    attr Caption
    blkCapt [ColSpec]
    specs TableHead
    thead [TableBody]
    tbodies TableFoot
    tfoot) =
      ([Inline] -> StateT WriterState m (Doc Text))
    -> ([Block] -> StateT WriterState m (Doc Text))
    -> Table
    -> StateT WriterState m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    ([Inline] -> LW m (Doc Text))
    -> ([Block] -> LW m (Doc Text)) -> Table -> LW m (Doc Text)
    tableToLaTeX [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Block] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX
                   ((Text, [Text], [(Text, Text)])
    -> Caption
    -> [ColSpec]
    -> TableHead
    -> [TableBody]
    -> TableFoot
    -> Table
    Ann.toTable (Text, [Text], [(Text, Text)])
    attr Caption
    blkCapt [ColSpec]
    specs TableHead
    thead [TableBody]
    tbodies TableFoot
    tfoot)
    blockToLaTeX (Figure (Text
    ident, [Text]
    _, [(Text, Text)]
    kvs) Caption
    captnode [Block]
    body) = do
      opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      (capt, captForLof, footnotes) <- getCaption inlineListToLaTeX True captnode
      lab <- labelFor ident
      let caption = Doc Text
    "\\caption" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    captForLof Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    capt Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    lab
          placement = case Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup Text
    "latex-placement" [(Text, Text)]
    kvs of
            Just Text
    p -> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (Text -> String
    T.unpack Text
    p))
            Maybe Text
    _      -> String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    ""

      isSubfigure <- gets stInFigure
      modify $ \WriterState
    st -> WriterState
    st{ stInFigure = True }
      contents <- case body of
        [Block
    b] -> Block -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX Block
    b
        [Block]
    bs  -> [Doc Text] -> Doc Text
    forall a. Monoid a => [a] -> a
    mconcat ([Doc Text] -> Doc Text)
    -> ([Doc Text] -> [Doc Text]) -> [Doc Text] -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Doc Text -> [Doc Text] -> [Doc Text]
    forall a. a -> [a] -> [a]
    intersperse (Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    "\\hfill") ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text]
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
               (Block -> StateT WriterState m (Doc Text))
    -> [Block] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
    mapM (Int -> Block -> StateT WriterState m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Int -> Block -> LW m (Doc Text)
    toSubfigure ([Block] -> Int
    forall a. [a] -> Int
    forall (t :: * -> *) a. Foldable t => t a -> Int
    length [Block]
    bs)) [Block]
    bs
      let innards = Doc Text
    "\\centering" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$
                    (case WriterOptions -> CaptionPosition
    writerFigureCaptionPosition WriterOptions
    opts of
                      CaptionPosition
    CaptionBelow -> Doc Text
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    caption
                      CaptionPosition
    CaptionAbove -> Doc Text
    caption Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    contents) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    cr
      modify $ \WriterState
    st ->
        WriterState
    st{ stInFigure = isSubfigure
          , stSubfigure = stSubfigure st || isSubfigure
          }

      let containsTable = Any -> Bool
    getAny (Any -> Bool) -> ([Block] -> Any) -> [Block] -> Bool
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (Block -> Any) -> [Block] -> Any
    forall c. Monoid c => (Block -> c) -> [Block] -> c
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (\case
            Table {}  -> Bool -> Any
    Any Bool
    True
            Block
    _         -> Bool -> Any
    Any Bool
    False)
      st <- get
      return $ (case () of
        ()
    _ | [Block] -> Bool
    containsTable [Block]
    body ->
              -- placing a longtable in a figure or center environment does
              -- not make sense.
              Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    contents
        ()
    _ | WriterState -> Bool
    stInMinipage WriterState
    st ->
              -- can't have figures in notes or minipage (here, table cell)
              -- http://www.tex.ac.uk/FAQ-ouparmd.html
              Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    "\\begin{center}" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    contents Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $+$ Doc Text
    capt Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    "\\end{center}"
        ()
    _ | Bool
    isSubfigure ->
              Doc Text
    innards
        ()
    _ ->  Doc Text
    forall a. Doc a
    cr Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    "\\begin{figure}" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    placement Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    innards Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    "\\end{figure}")
        $$ footnotes

    toSubfigure :: PandocMonad m => Int -> Block -> LW m (Doc Text)
    toSubfigure :: forall (m :: * -> *).
    PandocMonad m =>
    Int -> Block -> LW m (Doc Text)
    toSubfigure Int
    nsubfigs Block
    blk = do
      contents <- Block -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX Block
    blk
      let linewidth = forall a. Show a => a -> Text
    tshow @Double (Double
    0.9 Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Int -> Double
    forall a b. (Integral a, Num b) => a -> b
    fromIntegral Int
    nsubfigs) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    "\\linewidth"
      return $ cr <> case blk of
        Figure {}    -> [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat
                        [ Doc Text
    "\\begin{subfigure}[t]" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    linewidth)
                        , Doc Text
    contents
                        , Doc Text
    "\\end{subfigure}"
                        ]
        Block
    _            -> [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vcat
                        [ Doc Text
    "\\begin{minipage}[t]" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    linewidth)
                        , Doc Text
    contents
                        , Doc Text
    "\\end{minipage}"
                        ]

    blockListToLaTeX :: PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX :: forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    lst =
      [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    vsep ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text]
    -> StateT WriterState m (Doc Text)
    forall a b.
    (a -> b) -> StateT WriterState m a -> StateT WriterState m b
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    `fmap` (Block -> StateT WriterState m (Doc Text))
    -> [Block] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
    mapM (\Block
    b -> Bool -> LW m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    True LW m ()
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall a b.
    StateT WriterState m a
    -> StateT WriterState m b -> StateT WriterState m b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >> Block -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX Block
    b) [Block]
    lst

    listItemToLaTeX :: PandocMonad m => Bool -> [Block] -> LW m (Doc Text)
    listItemToLaTeX :: forall (m :: * -> *).
    PandocMonad m =>
    Bool -> [Block] -> LW m (Doc Text)
    listItemToLaTeX Bool
    isOrdered [Block]
    lst
      -- we need to put some text before a header if it's the first
      -- element in an item. This will look ugly in LaTeX regardless, but
      -- this will keep the typesetter from throwing an error.
      | (Header{} :[Block]
    _) <- [Block]
    lst =
        (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "\\item ~" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$) (Doc Text -> Doc Text)
    -> (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a
    nest Int
    2 (Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Block] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    lst
      | Bool -> Bool
    not Bool
    isOrdered
      , Just (Bool
    checked, [Block]
    bs) <- [Block] -> Maybe (Bool, [Block])
    forall (m :: * -> *). MonadPlus m => [Block] -> m (Bool, [Block])
    toTaskListItem [Block]
    lst
       = Bool -> [Block] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    Bool -> [Block] -> LW m (Doc Text)
    taskListItem Bool
    checked [Block]
    bs
      | Bool
    otherwise = (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "\\item" Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$) (Doc Text -> Doc Text)
    -> (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Int -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a
    nest Int
    2 (Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Block] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    lst
      where
        taskListItem :: Bool -> [Block] -> StateT WriterState m (Doc Text)
    taskListItem Bool
    checked [Block]
    bs = do
          let checkbox :: Doc Text
    checkbox  = if Bool
    checked
                          then Doc Text
    "$\\boxtimes$"
                          else Doc Text
    "$\\square$"
          let bs' :: [Block]
    bs' = case [Block]
    bs of
                      Plain [Inline]
    ils : [Block]
    xs -> [Inline] -> Block
    Para [Inline]
    ils Block -> [Block] -> [Block]
    forall a. a -> [a] -> [a]
    : [Block]
    xs
                      [Block]
    _ -> [Block]
    bs
          bsContents <- [Block] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Block] -> LW m (Doc Text)
    blockListToLaTeX [Block]
    bs'
          return $ "\\item" <> brackets checkbox $$ nest 2 bsContents

    defListItemToLaTeX :: PandocMonad m => ([Inline], [[Block]]) -> LW m (Doc Text)
    defListItemToLaTeX :: forall (m :: * -> *).
    PandocMonad m =>
    ([Inline], [[Block]]) -> LW m (Doc Text)
    defListItemToLaTeX ([Inline]
    term, [[Block]]
    defs) = do
        -- needed to turn off 'listings' because it breaks inside \item[...]:
        (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    s -> WriterState
    s{stInItem = True}
        term' <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    term
        modify $ \WriterState
    s -> WriterState
    s{stInItem = False}
        -- put braces around term if it contains an internal link,
        -- since otherwise we get bad bracket interactions: \item[\hyperref[..]
        let isInternalLink (Link (Text, [Text], [(Text, Text)])
    _ [Inline]
    _ (Text
    src,Text
    _))
              | Just (Char
    '#', Text
    _) <- Text -> Maybe (Char, Text)
    T.uncons Text
    src = Bool
    True
            isInternalLink Inline
    _                  = Bool
    False
        let term'' = if (Inline -> Bool) -> [Inline] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any Inline -> Bool
    isInternalLink [Inline]
    term
                        then Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    term'
                        else Doc Text
    term'
        def'  <- case concat defs of
                   [] -> Doc Text -> LW m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Monoid a => a
    mempty
                   (Block
    x:[Block]
    xs) -> do
                     (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    s -> WriterState
    s{stIsFirstInDefinition = True }
                     firstitem <- Block -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Block -> LW m (Doc Text)
    blockToLaTeX Block
    x
                     modify $ \WriterState
    s -> WriterState
    s{stIsFirstInDefinition = False }
                     rest <- blockListToLaTeX xs
                     return $ firstitem $+$ rest
        return $ case defs of
         ((Header{} : [Block]
    _) : [[Block]]
    _)    ->
           Doc Text
    "\\item" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets Doc Text
    term'' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    " ~ " Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    def'
         ((CodeBlock{} : [Block]
    _) : [[Block]]
    _) -> -- see #4662
           Doc Text
    "\\item" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets Doc Text
    term'' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    " ~ " Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    def'
         [[Block]]
    _                       ->
           Doc Text
    "\\item" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets Doc Text
    term'' Doc Text -> Doc Text -> Doc Text
    forall a. Doc a -> Doc a -> Doc a
    $$ Doc Text
    def'

    -- | Craft the section header, inserting the section reference, if supplied.
    sectionHeader :: PandocMonad m
                  => [Text]  -- classes
                  -> Text
                  -> Int
                  -> [Inline]
                  -> LW m (Doc Text)
    sectionHeader :: forall (m :: * -> *).
    PandocMonad m =>
    [Text] -> Text -> Int -> [Inline] -> LW m (Doc Text)
    sectionHeader [Text]
    classes Text
    ident Int
    level [Inline]
    lst = do
      let unnumbered :: Bool
    unnumbered = Text
    "unnumbered" Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
      let unlisted :: Bool
    unlisted = Text
    "unlisted" Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Text]
    classes
      txt <- [Inline] -> LW m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
      plain <- stringToLaTeX TextString $ T.concat $ map stringify lst
      let removeInvalidInline (Note [Block]
    _)             = []
          removeInvalidInline (Span (Text
    id', [Text]
    _, [(Text, Text)]
    _) [Inline]
    _) | Bool -> Bool
    not (Text -> Bool
    T.null Text
    id') = []
          removeInvalidInline Image{}            = []
          removeInvalidInline Inline
    x                    = [Inline
    x]
      let lstNoNotes = (Inline -> [Inline] -> [Inline])
    -> [Inline] -> [Inline] -> [Inline]
    forall a b. (a -> b -> b) -> b -> [a] -> b
    forall (t :: * -> *) a b.
    Foldable t =>
    (a -> b -> b) -> b -> t a -> b
    foldr ([Inline] -> [Inline] -> [Inline]
    forall a. Monoid a => a -> a -> a
    mappend ([Inline] -> [Inline] -> [Inline])
    -> (Inline -> [Inline]) -> Inline -> [Inline] -> [Inline]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (\Inline
    x -> (Inline -> [Inline]) -> Inline -> [Inline]
    forall a b (m :: * -> *).
    (Walkable a b, Monad m, Applicative m, Functor m) =>
    (a -> m a) -> b -> m b
    forall (m :: * -> *).
    (Monad m, Applicative m, Functor m) =>
    (Inline -> m Inline) -> Inline -> m Inline
    walkM Inline -> [Inline]
    removeInvalidInline Inline
    x)) [Inline]
    forall a. Monoid a => a
    mempty [Inline]
    lst
      txtNoNotes <- inlineListToLaTeX lstNoNotes
      txtNoLinksNoNotes <- inlineListToLaTeX (removeLinks lstNoNotes)
      -- footnotes in sections don't work (except for starred variants)
      -- unless you specify an optional argument:
      -- \section[mysec]{mysec\footnote{blah}}
      optional <- if unnumbered || lstNoNotes == lst || null lstNoNotes
                     then return empty
                     else
                       return $ brackets txtNoNotes
      let contents = if Maybe Int -> Doc Text -> Text
    forall a. HasChars a => Maybe Int -> Doc a -> a
    render Maybe Int
    forall a. Maybe a
    Nothing Doc Text
    txt Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    plain
                        then Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    txt
                        else Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "\\texorpdfstring"
                             Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    txt
                             Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    plain))
      book <- gets stHasChapters
      opts <- gets stOptions
      let topLevelDivision = if Bool
    book Bool -> Bool -> Bool
    && WriterOptions -> TopLevelDivision
    writerTopLevelDivision WriterOptions
    opts TopLevelDivision -> TopLevelDivision -> Bool
    forall a. Eq a => a -> a -> Bool
    == TopLevelDivision
    TopLevelDefault
                             then TopLevelDivision
    TopLevelChapter
                             else WriterOptions -> TopLevelDivision
    writerTopLevelDivision WriterOptions
    opts
      beamer <- gets stBeamer
      let level' = if Bool
    beamer Bool -> Bool -> Bool
    &&
                      TopLevelDivision
    topLevelDivision TopLevelDivision -> [TopLevelDivision] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [TopLevelDivision
    TopLevelPart, TopLevelDivision
    TopLevelChapter]
                   -- beamer has parts but no chapters
                   then if Int
    level Int -> Int -> Bool
    forall a. Eq a => a -> a -> Bool
    == Int
    1 then -Int
    1 else Int
    level Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
                   else case TopLevelDivision
    topLevelDivision of
                          TopLevelDivision
    TopLevelPart    -> Int
    level Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    2
                          TopLevelDivision
    TopLevelChapter -> Int
    level Int -> Int -> Int
    forall a. Num a => a -> a -> a
    - Int
    1
                          TopLevelDivision
    TopLevelSection -> Int
    level
                          TopLevelDivision
    TopLevelDefault -> Int
    level
      let sectionType = case Int
    level' of
                              -1 -> String
    "part"
                              Int
    0  -> String
    "chapter"
                              Int
    1  -> String
    "section"
                              Int
    2  -> String
    "subsection"
                              Int
    3  -> String
    "subsubsection"
                              Int
    4  -> String
    "paragraph"
                              Int
    5  -> String
    "subparagraph"
                              Int
    _  -> String
    ""
      inQuote <- gets stInQuote
      let prefix = if Bool
    inQuote
                      then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "\\mbox{}%"
                      -- needed for \paragraph, \subparagraph in quote environment
                      -- see http://tex.stackexchange.com/questions/169830/
                      else Doc Text
    forall a. Doc a
    empty
      lab <- labelFor ident
      let star = if Bool
    unnumbered then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "*" else Doc Text
    forall a. Doc a
    empty
      let title = Doc Text
    star Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    optional Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    contents
      return $ if level' > 5
                  then txt
                  else prefix
                       $$ text ('\\':sectionType) <> title <> lab
                       $$ if unnumbered && not unlisted
                             then "\\addcontentsline{toc}" <>
                                    braces (text sectionType) <>
                                    braces txtNoLinksNoNotes
                             else empty

    -- | Convert list of inline elements to LaTeX.
    inlineListToLaTeX :: PandocMonad m
                      => [Inline]  -- ^ Inlines to convert
                      -> LW m (Doc Text)
    inlineListToLaTeX :: forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst = [Doc Text] -> Doc Text
    forall a. [Doc a] -> Doc a
    hcat ([Doc Text] -> Doc Text)
    -> StateT WriterState m [Doc Text]
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$>
      (Inline -> StateT WriterState m (Doc Text))
    -> [Inline] -> StateT WriterState m [Doc Text]
    forall (t :: * -> *) (m :: * -> *) a b.
    (Traversable t, Monad m) =>
    (a -> m b) -> t a -> m (t b)
    forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
    mapM Inline -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Inline -> LW m (Doc Text)
    inlineToLaTeX
        ([Inline] -> [Inline]
    addKerns ([Inline] -> [Inline])
    -> ([Inline] -> [Inline]) -> [Inline] -> [Inline]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Inline] -> [Inline]
    fixLineInitialSpaces ([Inline] -> [Inline])
    -> ([Inline] -> [Inline]) -> [Inline] -> [Inline]
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . [Inline] -> [Inline]
    fixInitialLineBreaks ([Inline] -> [Inline]) -> [Inline] -> [Inline]
    forall a b. (a -> b) -> a -> b
    $ [Inline]
    lst)
        -- nonbreaking spaces (~) in LaTeX don't work after line breaks,
        -- so we insert a strut: this is mostly used in verse.
     where fixLineInitialSpaces :: [Inline] -> [Inline]
    fixLineInitialSpaces [] = []
           fixLineInitialSpaces (Inline
    LineBreak : Str Text
    s : [Inline]
    xs)
             | Just (Char
    '\160', Text
    _) <- Text -> Maybe (Char, Text)
    T.uncons Text
    s
             = Inline
    LineBreak Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : Format -> Text -> Inline
    RawInline Format
    "latex" Text
    "\\strut " Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : Text -> Inline
    Str Text
    s
                Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    fixLineInitialSpaces [Inline]
    xs
           fixLineInitialSpaces (Inline
    x:[Inline]
    xs) = Inline
    x Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    fixLineInitialSpaces [Inline]
    xs
           -- We need \hfill\break for a line break at the start
           -- of a paragraph. See #5591.
           fixInitialLineBreaks :: [Inline] -> [Inline]
    fixInitialLineBreaks (Inline
    LineBreak:[Inline]
    xs) =
             Format -> Text -> Inline
    RawInline (Text -> Format
    Format Text
    "latex") Text
    "\\hfill\\break\n" Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    :
               [Inline] -> [Inline]
    fixInitialLineBreaks [Inline]
    xs
           fixInitialLineBreaks [Inline]
    xs = [Inline]
    xs
           addKerns :: [Inline] -> [Inline]
    addKerns [] = []
           addKerns (Str Text
    s : q :: Inline
    q@Quoted{} : [Inline]
    rest)
             | Text -> Bool
    forall {a}. (Eq a, IsString a) => a -> Bool
    isQuote (Int -> Text -> Text
    T.takeEnd Int
    1 Text
    s) =
               Text -> Inline
    Str Text
    s Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : Format -> Text -> Inline
    RawInline (Text -> Format
    Format Text
    "latex") Text
    "\\," Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    addKerns (Inline
    qInline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    :[Inline]
    rest)
           addKerns (q :: Inline
    q@Quoted{} : Str Text
    s : [Inline]
    rest)
             | Text -> Bool
    forall {a}. (Eq a, IsString a) => a -> Bool
    isQuote (Int -> Text -> Text
    T.take Int
    1 Text
    s) =
               Inline
    q Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : Format -> Text -> Inline
    RawInline (Text -> Format
    Format Text
    "latex") Text
    "\\," Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    addKerns (Text -> Inline
    Str Text
    s Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline]
    rest)
           addKerns (Inline
    x:[Inline]
    xs) = Inline
    x Inline -> [Inline] -> [Inline]
    forall a. a -> [a] -> [a]
    : [Inline] -> [Inline]
    addKerns [Inline]
    xs
           isQuote :: a -> Bool
    isQuote a
    "\"" = Bool
    True
           isQuote a
    "'" = Bool
    True
           isQuote a
    "\x2018" = Bool
    True
           isQuote a
    "\x2019" = Bool
    True
           isQuote a
    "\x201C" = Bool
    True
           isQuote a
    "\x201D" = Bool
    True
           isQuote a
    _ = Bool
    False

    -- | Convert inline element to LaTeX
    inlineToLaTeX :: PandocMonad m
                  => Inline    -- ^ Inline to convert
                  -> LW m (Doc Text)
    inlineToLaTeX :: forall (m :: * -> *). PandocMonad m => Inline -> LW m (Doc Text)
    inlineToLaTeX (Span (Text
    "",[Text
    "mark"],[]) [Inline]
    lst) = do
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    st -> WriterState
    st{ stStrikeout = True } -- this gives us the soul package
      Text -> Doc Text -> Doc Text
    inCmd Text
    "hl" (Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> StateT WriterState m (Doc Text) -> StateT WriterState m (Doc Text)
    forall (m :: * -> *) a. PandocMonad m => LW m a -> LW m a
    inSoulCommand ([Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst)
    inlineToLaTeX (Span (Text
    id',[Text]
    classes,[(Text, Text)]
    kvs) [Inline]
    ils) = do
      linkAnchor <- Text -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Text -> LW m (Doc Text)
    hypertarget Text
    id'
      lang <- toLang $ lookup "lang" kvs
      let classToCmd a
    "csl-no-emph" = a -> Maybe a
    forall a. a -> Maybe a
    Just a
    "textup"
          classToCmd a
    "csl-no-strong" = a -> Maybe a
    forall a. a -> Maybe a
    Just a
    "textnormal"
          classToCmd a
    "csl-no-smallcaps" = a -> Maybe a
    forall a. a -> Maybe a
    Just a
    "textnormal"
          classToCmd a
    "csl-block" = a -> Maybe a
    forall a. a -> Maybe a
    Just a
    "CSLBlock"
          classToCmd a
    "csl-left-margin" = a -> Maybe a
    forall a. a -> Maybe a
    Just a
    "CSLLeftMargin"
          classToCmd a
    "csl-right-inline" = a -> Maybe a
    forall a. a -> Maybe a
    Just a
    "CSLRightInline"
          classToCmd a
    "csl-indent" = a -> Maybe a
    forall a. a -> Maybe a
    Just a
    "CSLIndent"
          classToCmd a
    _ = Maybe a
    forall a. Maybe a
    Nothing
          kvToCmd (a
    "dir",a
    "rtl") = a -> Maybe a
    forall a. a -> Maybe a
    Just a
    "RL"
          kvToCmd (a
    "dir",a
    "ltr") = a -> Maybe a
    forall a. a -> Maybe a
    Just a
    "LR"
          kvToCmd (a, a)
    _ = Maybe a
    forall a. Maybe a
    Nothing
          langCmds =
            case Maybe Lang
    lang Maybe Lang -> (Lang -> Maybe Text) -> Maybe Text
    forall a b. Maybe a -> (a -> Maybe b) -> Maybe b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= Lang -> Maybe Text
    toBabel of
               Just Text
    l  -> [Text
    "foreignlanguage{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    l Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    "}"]
               Maybe Text
    Nothing -> []
      let cmds = (Text -> Maybe Text) -> [Text] -> [Text]
    forall a b. (a -> Maybe b) -> [a] -> [b]
    mapMaybe Text -> Maybe Text
    forall {a} {a}. (Eq a, IsString a, IsString a) => a -> Maybe a
    classToCmd [Text]
    classes [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++ ((Text, Text) -> Maybe Text) -> [(Text, Text)] -> [Text]
    forall a b. (a -> Maybe b) -> [a] -> [b]
    mapMaybe (Text, Text) -> Maybe Text
    forall {a} {a} {a}.
    (Eq a, Eq a, IsString a, IsString a, IsString a) =>
    (a, a) -> Maybe a
    kvToCmd [(Text, Text)]
    kvs [Text] -> [Text] -> [Text]
    forall a. [a] -> [a] -> [a]
    ++ [Text]
    langCmds
      contents <- inlineListToLaTeX ils
      return $
        (if "csl-right-inline" `elem` classes
            then ("%" <>) -- see #7932
            else id) $
        (if any (`elem` classes)
                ["csl-block","csl-left-margin","csl-right-inline","csl-indent"]
            then (cr <>)
            else id) $
        (if T.null id'
            then empty
            else linkAnchor) <>
        (if null cmds
            then braces contents
            else foldr inCmd contents cmds)
    inlineToLaTeX (Emph [Inline]
    lst) = Text -> Doc Text -> Doc Text
    inCmd Text
    "emph" (Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    inlineToLaTeX (Underline [Inline]
    lst) = do
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    st -> WriterState
    st{ stStrikeout = True } -- this gives us the soul package
      Text -> Doc Text -> Doc Text
    inCmd Text
    "ul" (Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> StateT WriterState m (Doc Text) -> StateT WriterState m (Doc Text)
    forall (m :: * -> *) a. PandocMonad m => LW m a -> LW m a
    inSoulCommand ([Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst)
    inlineToLaTeX (Strong [Inline]
    lst) = Text -> Doc Text -> Doc Text
    inCmd Text
    "textbf" (Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    inlineToLaTeX (Strikeout [Inline]
    lst) = do
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    s -> WriterState
    s{ stStrikeout = True }
      Text -> Doc Text -> Doc Text
    inCmd Text
    "st" (Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> StateT WriterState m (Doc Text) -> StateT WriterState m (Doc Text)
    forall (m :: * -> *) a. PandocMonad m => LW m a -> LW m a
    inSoulCommand ([Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst)
    inlineToLaTeX (Superscript [Inline]
    lst) =
      Text -> Doc Text -> Doc Text
    inCmd Text
    "textsuperscript" (Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    inlineToLaTeX (Subscript [Inline]
    lst) =
      Text -> Doc Text -> Doc Text
    inCmd Text
    "textsubscript" (Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    inlineToLaTeX (SmallCaps [Inline]
    lst) =
      Text -> Doc Text -> Doc Text
    inCmd Text
    "textsc"(Doc Text -> Doc Text)
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
    inlineToLaTeX (Cite [Citation]
    cits [Inline]
    lst) = do
      opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      modify $ \WriterState
    st -> WriterState
    st{ stInCite = True }
      res <- case writerCiteMethod opts of
               CiteMethod
    Natbib   -> ([Inline] -> StateT WriterState m (Doc Text))
    -> [Citation] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    ([Inline] -> LW m (Doc Text)) -> [Citation] -> LW m (Doc Text)
    citationsToNatbib [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Citation]
    cits
               CiteMethod
    Biblatex -> ([Inline] -> StateT WriterState m (Doc Text))
    -> [Citation] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *).
    PandocMonad m =>
    ([Inline] -> LW m (Doc Text)) -> [Citation] -> LW m (Doc Text)
    citationsToBiblatex [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Citation]
    cits
               CiteMethod
    _        -> [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
      modify $ \WriterState
    st -> WriterState
    st{ stInCite = False }
      pure res

    inlineToLaTeX (Code (Text
    _,[Text]
    classes,[(Text, Text)]
    kvs) Text
    str) = do
      opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      inHeading <- gets stInHeading
      inItem <- gets stInItem
      inSoul <- gets stInSoulCommand
      inCaption <- gets stInCaption
      let listingsCode = do
            let listingsopts :: [(Text, Text)]
    listingsopts = (case [Text] -> Maybe Text
    getListingsLanguage [Text]
    classes of
                                    Just Text
    l  -> ((Text
    "language", Text -> Text
    mbBraced Text
    l)(Text, Text) -> [(Text, Text)] -> [(Text, Text)]
    forall a. a -> [a] -> [a]
    :)
                                    Maybe Text
    Nothing -> [(Text, Text)] -> [(Text, Text)]
    forall a. a -> a
    id)
                               [(Text
    k,Text
    v) | (Text
    k,Text
    v) <- [(Text, Text)]
    kvs
                                      , Text
    k Text -> [Text] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `notElem` [Text
    "exports",Text
    "tangle",Text
    "results"]]
            let listingsopt :: Text
    listingsopt = if [(Text, Text)] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [(Text, Text)]
    listingsopts
                                 then Text
    ""
                                 else Text
    "[" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <>
                                      Text -> [Text] -> Text
    T.intercalate Text
    ", "
                                      (((Text, Text) -> Text) -> [(Text, Text)] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map (\(Text
    k,Text
    v) -> Text
    k Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    "=" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    v)
                                       [(Text, Text)]
    listingsopts) Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    "]"
            inNote <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInNote
            when inNote $ modify $ \WriterState
    s -> WriterState
    s{ stVerbInNote = True }
            let chr = case String
    "!\"'()*,-./:;?@" String -> String -> String
    forall a. Eq a => [a] -> [a] -> [a]
    \\ Text -> String
    T.unpack Text
    str of
                           (Char
    c:String
    _) -> Char
    c
                           []    -> Char
    '!'
            let isEscapable Char
    '\\' = Bool
    True
                isEscapable Char
    '{'  = Bool
    True
                isEscapable Char
    '}'  = Bool
    True
                isEscapable Char
    '%'  = Bool
    True
                isEscapable Char
    '~'  = Bool
    True
                isEscapable Char
    '_'  = Bool
    True
                isEscapable Char
    '&'  = Bool
    True
                isEscapable Char
    '#'  = Bool
    True
                isEscapable Char
    '^'  = Bool
    True
                isEscapable Char
    _    = Bool
    False
            let escChar Char
    c | Char -> Bool
    isEscapable Char
    c = String -> Text
    T.pack [Char
    '\\',Char
    c]
                          | Bool
    otherwise     = Char -> Text
    T.singleton Char
    c
            let str' = (Char -> Text) -> Text -> Text
    T.concatMap Char -> Text
    escChar Text
    str
            -- we always put lstinline in a dummy 'passthrough' command
            -- (defined in the default template) so that we don't have
            -- to change the way we escape characters depending on whether
            -- the lstinline is inside another command.  See #1629:
            return $ literal $ "\\passthrough{\\lstinline" <>
                            listingsopt <> T.singleton chr <> str' <> T.singleton chr <> "}"
      let rawCode = (Text -> Doc Text)
    -> StateT WriterState m Text -> StateT WriterState m (Doc Text)
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Doc Text) -> (Text -> Text) -> Text -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . (\Text
    s -> Text
    "\\texttt{" Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Text
    escapeSpaces Text
    s Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    "}"))
                     (StateT WriterState m Text -> StateT WriterState m (Doc Text))
    -> StateT WriterState m Text -> StateT WriterState m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    CodeString Text
    str
                    where escapeSpaces :: Text -> Text
    escapeSpaces = (Char -> Text) -> Text -> Text
    T.concatMap
                             (\Char
    c -> if Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == Char
    ' ' then Text
    "\\ " else Char -> Text
    T.singleton Char
    c)
      let highlightCode =
            case SyntaxMap
    -> (FormatOptions -> [SourceLine] -> Text)
    -> (Text, [Text], [(Text, Text)])
    -> Text
    -> Either Text Text
    forall a.
    SyntaxMap
    -> (FormatOptions -> [SourceLine] -> a)
    -> (Text, [Text], [(Text, Text)])
    -> Text
    -> Either Text a
    highlight (WriterOptions -> SyntaxMap
    writerSyntaxMap WriterOptions
    opts)
                     FormatOptions -> [SourceLine] -> Text
    formatLaTeXInline (Text
    "",[Text]
    classes,[]) Text
    str of
                   Left Text
    msg -> do
                     Bool -> StateT WriterState m () -> StateT WriterState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    unless (Text -> Bool
    T.null Text
    msg) (StateT WriterState m () -> StateT WriterState m ())
    -> StateT WriterState m () -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ LogMessage -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (LogMessage -> StateT WriterState m ())
    -> LogMessage -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ Text -> LogMessage
    CouldNotHighlight Text
    msg
                     StateT WriterState m (Doc Text)
    rawCode
                   Right Text
    h -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify (\WriterState
    st -> WriterState
    st{ stHighlighting = True }) StateT WriterState m ()
    -> StateT WriterState m (Doc Text)
    -> StateT WriterState m (Doc Text)
    forall a b.
    StateT WriterState m a
    -> StateT WriterState m b -> StateT WriterState m b
    forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
    >>
                              Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return (String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (Text -> String
    T.unpack Text
    h))
      -- for soul commands we need to protect VERB in an mbox or we get an error
      -- (see #1294). with regular texttt we don't get an error, but we get
      -- incorrect results if there is a space (see #5529).
      let inMbox Doc a
    x = Doc a
    "\\mbox" Doc a -> Doc a -> Doc a
    forall a. Semigroup a => a -> a -> a
    <> Doc a -> Doc a
    forall a. HasChars a => Doc a -> Doc a
    braces Doc a
    x

      -- for captions we need to protect VERB with \protect (see #6821)
      let protect a
    x = a
    "\\protect" a -> a -> a
    forall a. Semigroup a => a -> a -> a
    <> a
    x

      let optionalProtect = case () of ()
    _ | Bool
    inSoul -> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    inMbox
                                         | Bool
    inCaption -> Doc Text -> Doc Text
    forall {a}. (Semigroup a, IsString a) => a -> a
    protect
                                         | Bool
    otherwise -> Doc Text -> Doc Text
    forall a. a -> a
    id
      optionalProtect <$>
       case writerHighlightMethod opts of
         HighlightMethod
    _ | Bool
    inHeading Bool -> Bool -> Bool
    || Bool
    inItem  -> StateT WriterState m (Doc Text)
    rawCode  -- see #5574
         HighlightMethod
    IdiomaticHighlighting    -> StateT WriterState m (Doc Text)
    listingsCode
         Skylighting Style
    _ | Bool -> Bool
    not ([Text] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    classes)
                                  -> StateT WriterState m (Doc Text)
    highlightCode
         HighlightMethod
    DefaultHighlighting | Bool -> Bool
    not ([Text] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Text]
    classes)
                                  -> StateT WriterState m (Doc Text)
    highlightCode
         HighlightMethod
    _noHighlighting          -> StateT WriterState m (Doc Text)
    rawCode
    inlineToLaTeX (Quoted QuoteType
    qt [Inline]
    lst) = do
      contents <- [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    lst
      csquotes <- liftM stCsquotes get
      opts <- gets stOptions
      if csquotes
         then return $ case qt of
                   QuoteType
    DoubleQuote -> Doc Text
    "\\enquote" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    contents
                   QuoteType
    SingleQuote -> Doc Text
    "\\enquote*" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces Doc Text
    contents
         else do
           let endsWithQuote [Inline]
    xs =
                 case [Inline] -> [Inline]
    forall a. [a] -> [a]
    reverse [Inline]
    xs of
                       Quoted{}:[Inline]
    _ -> Bool
    True
                       Span (Text, [Text], [(Text, Text)])
    _ [Inline]
    ys : [Inline]
    _ -> [Inline] -> Bool
    endsWithQuote [Inline]
    ys
                       Str Text
    s:[Inline]
    _ -> Int -> Text -> Text
    T.takeEnd Int
    1 Text
    s Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    "'"
                       [Inline]
    _ -> Bool
    False
           let beginsWithQuote [Inline]
    xs =
                 case [Inline]
    xs of
                       Quoted{}:[Inline]
    _ -> Bool
    True
                       Span (Text, [Text], [(Text, Text)])
    _ [Inline]
    ys : [Inline]
    _ -> [Inline] -> Bool
    beginsWithQuote [Inline]
    ys
                       Str Text
    s:[Inline]
    _ -> Int -> Text -> Text
    T.take Int
    1 Text
    s Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    "`"
                       [Inline]
    _ -> Bool
    False
           let inner = (if [Inline] -> Bool
    beginsWithQuote [Inline]
    lst then Doc Text
    "\\," else Doc Text
    forall a. Monoid a => a
    mempty)
                        Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    contents
                        Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> (if [Inline] -> Bool
    endsWithQuote [Inline]
    lst then Doc Text
    "\\," else Doc Text
    forall a. Monoid a => a
    mempty)
           return $ case qt of
                    QuoteType
    DoubleQuote ->
                       if Extension -> WriterOptions -> Bool
    forall a. HasSyntaxExtensions a => Extension -> a -> Bool
    isEnabled Extension
    Ext_smart WriterOptions
    opts
                          then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "``" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    inner Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "''"
                          else Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char Char
    '\x201C' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    inner Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char Char
    '\x201D'
                    QuoteType
    SingleQuote ->
                       if Extension -> WriterOptions -> Bool
    forall a. HasSyntaxExtensions a => Extension -> a -> Bool
    isEnabled Extension
    Ext_smart WriterOptions
    opts
                          then Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char Char
    '`' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    inner Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char Char
    '\''
                          else Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char Char
    '\x2018' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    inner Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Char -> Doc Text
    forall a. HasChars a => Char -> Doc a
    char Char
    '\x2019'
    inlineToLaTeX (Str Text
    str) = do
      Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
      (Text -> Doc Text)
    -> StateT WriterState m Text -> StateT WriterState m (Doc Text)
    forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
    liftM Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (StateT WriterState m Text -> StateT WriterState m (Doc Text))
    -> StateT WriterState m Text -> StateT WriterState m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    TextString Text
    str
    inlineToLaTeX (Math MathType
    _ Text
    str)
      | Text -> Bool
    isMathEnv Text
    str -- see #9711
      = do Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
           Bool -> StateT WriterState m () -> StateT WriterState m ()
    forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
    when (Text -> Bool
    needsCancel Text
    str) (StateT WriterState m () -> StateT WriterState m ())
    -> StateT WriterState m () -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    st -> WriterState
    st{ stCancel = True }
           Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure (Doc Text -> StateT WriterState m (Doc Text))
    -> Doc Text -> StateT WriterState m (Doc Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    str
    inlineToLaTeX (Math MathType
    InlineMath Text
    str) = do
      Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
      inSoul <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInSoulCommand
      let contents = Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Text
    handleMathComment Text
    str)
      when (needsCancel str) $ modify $ \WriterState
    st -> WriterState
    st{ stCancel = True }
      return $
        if inSoul -- #9597
           then "$" <> contents <> "$"
           else "\\(" <> contents <> "\\)"
    inlineToLaTeX (Math MathType
    DisplayMath Text
    str) = do
      Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
      inSoul <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInSoulCommand
      let contents = Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Text -> Text
    handleMathComment Text
    str)
      when (needsCancel str) $ modify $ \WriterState
    st -> WriterState
    st{ stCancel = True }
      return $
        if inSoul -- # 9597
           then "$$" <> contents <> "$$"
           else "\\[" <> contents <> "\\]"
    inlineToLaTeX il :: Inline
    il@(RawInline Format
    f Text
    str) = do
      beamer <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stBeamer
      if f == Format "latex" || f == Format "tex" ||
            (f == Format "beamer" && beamer)
         then do
           setEmptyLine False
           return $ literal str
         else do
           report $ InlineNotRendered il
           return empty
    inlineToLaTeX Inline
    LineBreak = do
      emptyLine <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stEmptyLine
      setEmptyLine True
      return $ (if emptyLine then "\\strut " else "") <> "\\\\" <> cr
    inlineToLaTeX Inline
    SoftBreak = do
      wrapText <- (WriterState -> WrapOption) -> StateT WriterState m WrapOption
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets (WriterOptions -> WrapOption
    writerWrapText (WriterOptions -> WrapOption)
    -> (WriterState -> WriterOptions) -> WriterState -> WrapOption
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . WriterState -> WriterOptions
    stOptions)
      case wrapText of
           WrapOption
    WrapAuto     -> Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    space
           WrapOption
    WrapNone     -> Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    space
           WrapOption
    WrapPreserve -> Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    cr
    inlineToLaTeX Inline
    Space = Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    space
    inlineToLaTeX (Link (Text
    id',[Text]
    _,[(Text, Text)]
    _) [Inline]
    txt (Text
    src,Text
    _)) =
       (case Text -> Maybe (Char, Text)
    T.uncons Text
    src of
         Just (Char
    '#', Text
    ident) -> do
            contents <- [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    txt
            lab <- toLabel ident
            inCite <- gets stInCite
            beamer <- gets stBeamer
            return $
              if inCite && "#ref-" `T.isPrefixOf` src
                 then "\\citeproc" <> braces (literal lab) <> braces contents
                 else if beamer
                         then "\\hyperlink" <> braces (literal lab) <> braces contents
                         else "\\hyperref" <> brackets (literal lab) <> braces contents
         Maybe (Char, Text)
    _ -> case [Inline]
    txt of
              -- For soul sommands we need to protect \url and \href in an mbox or
              -- we get an error (see #9366)
              [Str Text
    x] | (Char -> Bool) -> Text -> Bool
    T.all Char -> Bool
    isAscii Text
    x  -- see #8802
                      , String -> String
    unEscapeString (Text -> String
    T.unpack Text
    x) String -> String -> Bool
    forall a. Eq a => a -> a -> Bool
    ==
                        String -> String
    unEscapeString (Text -> String
    T.unpack Text
    src) ->  -- autolink
                   do (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    s -> WriterState
    s{ stUrl = True }
                      src' <- StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    URLString (Text -> Text
    escapeURI Text
    src)
                      protectInMboxIfInSoul $ literal $ "\\url{" <> src' <> "}"
              [Str Text
    x] | Just Text
    rest <- Text -> Text -> Maybe Text
    T.stripPrefix Text
    "mailto:" Text
    src,
                        String -> String
    unEscapeString (Text -> String
    T.unpack Text
    x) String -> String -> Bool
    forall a. Eq a => a -> a -> Bool
    == String -> String
    unEscapeString (Text -> String
    T.unpack Text
    rest) -> -- email autolink
                   do (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    s -> WriterState
    s{ stUrl = True }
                      src' <- StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    URLString (Text -> Text
    escapeURI Text
    src)
                      contents <- inlineListToLaTeX txt
                      protectInMboxIfInSoul $ "\\href" <> braces (literal src') <>
                         braces ("\\nolinkurl" <> braces contents)
              [Inline]
    _ -> do contents <- [Inline] -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => [Inline] -> LW m (Doc Text)
    inlineListToLaTeX [Inline]
    txt
                      src' <- stringToLaTeX URLString (escapeURI src)
                      protectInMboxIfInSoul $ literal ("\\href{" <> src' <> "}{") <>
                        contents <> char '}')
         StateT WriterState m (Doc Text)
    -> (Doc Text -> StateT WriterState m (Doc Text))
    -> StateT WriterState m (Doc Text)
    forall a b.
    StateT WriterState m a
    -> (a -> StateT WriterState m b) -> StateT WriterState m b
    forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
    >>= (if Text -> Bool
    T.null Text
    id'
                 then Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return
                 else \Doc Text
    x -> do
                   linkAnchor <- Text -> StateT WriterState m (Doc Text)
    forall (m :: * -> *). PandocMonad m => Text -> LW m (Doc Text)
    hypertarget Text
    id'
                   return (linkAnchor <> x))
    inlineToLaTeX il :: Inline
    il@(Image (Text, [Text], [(Text, Text)])
    _ [Inline]
    _ (Text
    src, Text
    _))
      | Just Text
    _ <- Text -> Text -> Maybe Text
    T.stripPrefix Text
    "data:" Text
    src = do
          LogMessage -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (LogMessage -> StateT WriterState m ())
    -> LogMessage -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ Inline -> LogMessage
    InlineNotRendered Inline
    il
          Doc Text -> StateT WriterState m (Doc Text)
    forall a. a -> StateT WriterState m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return Doc Text
    forall a. Doc a
    empty
    inlineToLaTeX (Image attr :: (Text, [Text], [(Text, Text)])
    attr@(Text
    _,[Text]
    _,[(Text, Text)]
    kvs) [Inline]
    description (Text
    source, Text
    _)) = do
      Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
      let isSVG :: Bool
    isSVG = Text
    ".svg" Text -> Text -> Bool
    `T.isSuffixOf` Text
    source Bool -> Bool -> Bool
    || Text
    ".SVG" Text -> Text -> Bool
    `T.isSuffixOf` Text
    source
      (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    s -> WriterState
    s{ stGraphics = True
                      , stSVG = stSVG s || isSVG }
      opts <- (WriterState -> WriterOptions)
    -> StateT WriterState m WriterOptions
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> WriterOptions
    stOptions
      mbalt <- if isSVG
                  then pure Nothing
                  else case lookup "alt" kvs of
                         Just Text
    x -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just (Text -> Maybe Text)
    -> StateT WriterState m Text -> StateT WriterState m (Maybe Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    TextString Text
    x
                         Maybe Text
    Nothing
                           | [Inline] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Inline]
    description -> Maybe Text -> StateT WriterState m (Maybe Text)
    forall a. a -> StateT WriterState m a
    forall (f :: * -> *) a. Applicative f => a -> f a
    pure Maybe Text
    forall a. Maybe a
    Nothing
                           | Bool
    otherwise -> Text -> Maybe Text
    forall a. a -> Maybe a
    Just (Text -> Maybe Text)
    -> StateT WriterState m Text -> StateT WriterState m (Maybe Text)
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> StringContext -> Text -> StateT WriterState m Text
    forall (m :: * -> *).
    PandocMonad m =>
    StringContext -> Text -> LW m Text
    stringToLaTeX StringContext
    TextString
                                                      ([Inline] -> Text
    forall a. Walkable Inline a => a -> Text
    stringify [Inline]
    description)
      let showDim Direction
    dir = let d :: Doc Text
    d = String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (Direction -> String
    forall a. Show a => a -> String
    show Direction
    dir) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    "="
                        in case Direction -> (Text, [Text], [(Text, Text)]) -> Maybe Dimension
    dimension Direction
    dir (Text, [Text], [(Text, Text)])
    attr of
                             Just (Pixel Integer
    a)   ->
                               [Doc Text
    d Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (WriterOptions -> Dimension -> Text
    showInInch WriterOptions
    opts (Integer -> Dimension
    Pixel Integer
    a)) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    "in"]
                             Just (Percent Double
    a) ->
                               [Doc Text
    d Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal (Double -> Text
    forall a. RealFloat a => a -> Text
    showFl (Double
    a Double -> Double -> Double
    forall a. Fractional a => a -> a -> a
    / Double
    100)) Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>
                                 case Direction
    dir of
                                    Direction
    Width  -> Doc Text
    "\\linewidth"
                                    Direction
    Height -> Doc Text
    "\\textheight"
                               ]
                             Just Dimension
    dim         ->
                               [Doc Text
    d Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text (Dimension -> String
    forall a. Show a => a -> String
    show Dimension
    dim)]
                             Maybe Dimension
    Nothing          ->
                               case Direction
    dir of
                                    Direction
    Width | Maybe Dimension -> Bool
    forall a. Maybe a -> Bool
    isJust (Direction -> (Text, [Text], [(Text, Text)]) -> Maybe Dimension
    dimension Direction
    Height (Text, [Text], [(Text, Text)])
    attr) ->
                                      [Doc Text
    d Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    "\\linewidth"]
                                    Direction
    Height | Maybe Dimension -> Bool
    forall a. Maybe a -> Bool
    isJust (Direction -> (Text, [Text], [(Text, Text)]) -> Maybe Dimension
    dimension Direction
    Width (Text, [Text], [(Text, Text)])
    attr) ->
                                      [Doc Text
    d Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    "\\textheight"]
                                    Direction
    _ -> []
          optList = Direction -> [Doc Text]
    showDim Direction
    Width [Doc Text] -> [Doc Text] -> [Doc Text]
    forall a. Semigroup a => a -> a -> a
    <> Direction -> [Doc Text]
    showDim Direction
    Height [Doc Text] -> [Doc Text] -> [Doc Text]
    forall a. Semigroup a => a -> a -> a
    <>
                    (case (Direction -> (Text, [Text], [(Text, Text)]) -> Maybe Dimension
    dimension Direction
    Height (Text, [Text], [(Text, Text)])
    attr, Direction -> (Text, [Text], [(Text, Text)]) -> Maybe Dimension
    dimension Direction
    Width (Text, [Text], [(Text, Text)])
    attr) of
                      (Just Dimension
    _, Just Dimension
    _) -> []
                      (Maybe Dimension, Maybe Dimension)
    _ -> [Doc Text
    "keepaspectratio"]) [Doc Text] -> [Doc Text] -> [Doc Text]
    forall a. Semigroup a => a -> a -> a
    <>
                    [Doc Text] -> (Text -> [Doc Text]) -> Maybe Text -> [Doc Text]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] (\Text
    x -> [Doc Text
    "page=" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    x]) (Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup Text
    "page" [(Text, Text)]
    kvs) [Doc Text] -> [Doc Text] -> [Doc Text]
    forall a. Semigroup a => a -> a -> a
    <>
                    [Doc Text] -> (Text -> [Doc Text]) -> Maybe Text -> [Doc Text]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] (\Text
    x -> [Doc Text
    "trim=" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    x]) (Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup Text
    "trim" [(Text, Text)]
    kvs) [Doc Text] -> [Doc Text] -> [Doc Text]
    forall a. Semigroup a => a -> a -> a
    <>
                    [Doc Text] -> (Text -> [Doc Text]) -> Maybe Text -> [Doc Text]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] (\Text
    x -> [Doc Text
    "alt=" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces (Text -> Doc Text
    forall a. HasChars a => a -> Doc a
    literal Text
    x)]) Maybe Text
    mbalt [Doc Text] -> [Doc Text] -> [Doc Text]
    forall a. Semigroup a => a -> a -> a
    <>
                    [Doc Text] -> (Text -> [Doc Text]) -> Maybe Text -> [Doc Text]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] ([Doc Text] -> Text -> [Doc Text]
    forall a b. a -> b -> a
    const [Doc Text
    "clip"]) (Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup Text
    "clip" [(Text, Text)]
    kvs)
          options = if [Doc Text] -> Bool
    forall a. [a] -> Bool
    forall (t :: * -> *) a. Foldable t => t a -> Bool
    null [Doc Text]
    optList
                       then Doc Text
    forall a. Doc a
    empty
                       else Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    brackets (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall a b. (a -> b) -> a -> b
    $ [Doc Text] -> Doc Text
    forall a. Monoid a => [a] -> a
    mconcat (Doc Text -> [Doc Text] -> [Doc Text]
    forall a. a -> [a] -> [a]
    intersperse Doc Text
    "," [Doc Text]
    optList)
          source' = if Text -> Bool
    isURI Text
    source
                       then Text
    source
                       else String -> Text
    T.pack (String -> Text) -> String -> Text
    forall a b. (a -> b) -> a -> b
    $ String -> String
    unEscapeString (String -> String) -> String -> String
    forall a b. (a -> b) -> a -> b
    $ Text -> String
    T.unpack Text
    source
      source'' <- stringToLaTeX URLString source'
      inHeading <- gets stInHeading
      return $
        (if inHeading then "\\protect" else "") <>
        (case dimension Width attr `mplus` dimension Height attr of
           Just Dimension
    _ -> Doc Text -> Doc Text
    forall a. a -> a
    id
           Maybe Dimension
    Nothing -> (Doc Text
    "\\pandocbounded" Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <>) (Doc Text -> Doc Text)
    -> (Doc Text -> Doc Text) -> Doc Text -> Doc Text
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Doc Text -> Doc Text
    forall a. HasChars a => Doc a -> Doc a
    braces)
          ((if isSVG then "\\includesvg" else "\\includegraphics") <>
            options <> braces (literal source''))
    inlineToLaTeX (Note [Block]
    contents) = do
      Bool -> StateT WriterState m ()
    forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    False
      externalNotes <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stExternalNotes
      modify (\WriterState
    s -> WriterState
    s{stInNote = True, stExternalNotes = True})
      contents' <- blockListToLaTeX contents
      modify (\WriterState
    s -> WriterState
    s {stInNote = False, stExternalNotes = externalNotes})
      let optnl = case [Block] -> [Block]
    forall a. [a] -> [a]
    reverse [Block]
    contents of
                       (CodeBlock (Text, [Text], [(Text, Text)])
    _ Text
    _ : [Block]
    _) -> Doc a
    forall a. Doc a
    cr
                       [Block]
    _                   -> Doc a
    forall a. Doc a
    empty
      let noteContents = Int -> Doc Text -> Doc Text
    forall a. IsString a => Int -> Doc a -> Doc a
    nest Int
    2 Doc Text
    contents' Doc Text -> Doc Text -> Doc Text
    forall a. Semigroup a => a -> a -> a
    <> Doc Text
    forall a. Doc a
    optnl
      beamer <- gets stBeamer
      -- in beamer slides, display footnote from current overlay forward
      -- and ensure that the note is on the frame, not e.g. the column (#5769, #5954)
      incremental <- gets stIncremental
      let beamerMark = if Bool
    beamer
                          then if Bool
    incremental
                               then String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "<.->[frame]"
                               else String -> Doc Text
    forall a. HasChars a => String -> Doc a
    text String
    "<\\value{beamerpauses}->[frame]"
                          else Doc Text
    forall a. Doc a
    empty
      if externalNotes
         then do
           modify $ \WriterState
    st -> WriterState
    st{ stNotes = noteContents : stNotes st }
           return "\\footnotemark{}"
           -- note: a \n before } needed when note ends with a Verbatim environment
           else return $ "\\footnote" <> beamerMark <> braces noteContents

    -- A comment at the end of math needs to be followed by a newline,
    -- or the closing delimiter gets swallowed.
    handleMathComment :: Text -> Text
    handleMathComment :: Text -> Text
    handleMathComment Text
    s =
      let (Text
    _, Text
    ys) = (Char -> Bool) -> Text -> (Text, Text)
    T.break (\Char
    c -> Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == Char
    '\n' Bool -> Bool -> Bool
    || Char
    c Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    == Char
    '%') (Text -> (Text, Text)) -> Text -> (Text, Text)
    forall a b. (a -> b) -> a -> b
    $ Text -> Text
    T.reverse Text
    s -- no T.breakEnd
      in  case Text -> Maybe (Char, Text)
    T.uncons Text
    ys of
            Just (Char
    '%', Text
    ys') -> case Text -> Maybe (Char, Text)
    T.uncons Text
    ys' of
              Just (Char
    '\\', Text
    _) -> Text
    s
              Maybe (Char, Text)
    _              -> Text
    s Text -> Text -> Text
    forall a. Semigroup a => a -> a -> a
    <> Text
    "\n"
            Maybe (Char, Text)
    _                -> Text
    s

    setEmptyLine :: PandocMonad m => Bool -> LW m ()
    setEmptyLine :: forall (m :: * -> *). PandocMonad m => Bool -> LW m ()
    setEmptyLine Bool
    b = (WriterState -> WriterState) -> StateT WriterState m ()
    forall s (m :: * -> *). MonadState s m => (s -> s) -> m ()
    modify ((WriterState -> WriterState) -> StateT WriterState m ())
    -> (WriterState -> WriterState) -> StateT WriterState m ()
    forall a b. (a -> b) -> a -> b
    $ \WriterState
    st -> WriterState
    st{ stEmptyLine = b }

    -- Extract a key from divs and spans
    extract :: Text -> Block -> [Text]
    extract :: Text -> Block -> [Text]
    extract Text
    key (Div (Text, [Text], [(Text, Text)])
    attr [Block]
    _)     = Text -> (Text, [Text], [(Text, Text)]) -> [Text]
    lookKey Text
    key (Text, [Text], [(Text, Text)])
    attr
    extract Text
    key (Plain [Inline]
    ils)      = (Inline -> [Text]) -> [Inline] -> [Text]
    forall c. Monoid c => (Inline -> c) -> [Inline] -> c
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (Text -> Inline -> [Text]
    extractInline Text
    key) [Inline]
    ils
    extract Text
    key (Para [Inline]
    ils)       = (Inline -> [Text]) -> [Inline] -> [Text]
    forall c. Monoid c => (Inline -> c) -> [Inline] -> c
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (Text -> Inline -> [Text]
    extractInline Text
    key) [Inline]
    ils
    extract Text
    key (Header Int
    _ (Text, [Text], [(Text, Text)])
    _ [Inline]
    ils) = (Inline -> [Text]) -> [Inline] -> [Text]
    forall c. Monoid c => (Inline -> c) -> [Inline] -> c
    forall a b c. (Walkable a b, Monoid c) => (a -> c) -> b -> c
    query (Text -> Inline -> [Text]
    extractInline Text
    key) [Inline]
    ils
    extract Text
    _ Block
    _                  = []

    -- Extract a key from spans
    extractInline :: Text -> Inline -> [Text]
    extractInline :: Text -> Inline -> [Text]
    extractInline Text
    key (Span (Text, [Text], [(Text, Text)])
    attr [Inline]
    _) = Text -> (Text, [Text], [(Text, Text)]) -> [Text]
    lookKey Text
    key (Text, [Text], [(Text, Text)])
    attr
    extractInline Text
    _ Inline
    _               = []

    -- Look up a key in an attribute and give a list of its values
    lookKey :: Text -> Attr -> [Text]
    lookKey :: Text -> (Text, [Text], [(Text, Text)]) -> [Text]
    lookKey Text
    key (Text
    _,[Text]
    _,[(Text, Text)]
    kvs) =  [Text] -> (Text -> [Text]) -> Maybe Text -> [Text]
    forall b a. b -> (a -> b) -> Maybe a -> b
    maybe [] Text -> [Text]
    T.words (Maybe Text -> [Text]) -> Maybe Text -> [Text]
    forall a b. (a -> b) -> a -> b
    $ Text -> [(Text, Text)] -> Maybe Text
    forall a b. Eq a => a -> [(a, b)] -> Maybe b
    lookup Text
    key [(Text, Text)]
    kvs

    -- soul doesn't like \(..\) delimiters, so we change these to $ (#9597)
    -- when processing their contents.
    inSoulCommand :: PandocMonad m => LW m a -> LW m a
    inSoulCommand :: forall (m :: * -> *) a. PandocMonad m => LW m a -> LW m a
    inSoulCommand LW m a
    pa = do
      oldInSoulCommand <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInSoulCommand
      modify $ \WriterState
    st -> WriterState
    st{ stInSoulCommand = True }
      result <- pa
      modify $ \WriterState
    st -> WriterState
    st{ stInSoulCommand = oldInSoulCommand }
      pure result

    -- Inside soul commands some commands need to be protected in an mbox
    -- or we get an error (e.g. see #1294)
    protectInMboxIfInSoul :: (PandocMonad m, HasChars a) => Doc a -> LW m (Doc a)
    protectInMboxIfInSoul :: forall (m :: * -> *) a.
    (PandocMonad m, HasChars a) =>
    Doc a -> LW m (Doc a)
    protectInMboxIfInSoul Doc a
    command = do
      inSoul <- (WriterState -> Bool) -> StateT WriterState m Bool
    forall s (m :: * -> *) a. MonadState s m => (s -> a) -> m a
    gets WriterState -> Bool
    stInSoulCommand
      return $ if inSoul
        then "\\mbox" <> braces command
        else command

    -- Babel languages with a .ldf that works well with all engines (see #8283).
    -- We follow the guidance from the Babel documentation:
    -- "In general, you should do this for European languages written in Latin
    -- and Cyrillic scripts, as well as for Vietnamese."
    ldfLanguages :: [Text]
    ldfLanguages :: [Text]
    ldfLanguages =
      [ Text
    "magyar"
      , Text
    "croatian"
      , Text
    "ngerman"
      , Text
    "germanb"
      , Text
    "german"
      , Text
    "austrian"
      , Text
    "ngermanb"
      , Text
    "naustrian"
      , Text
    "nswissgerman"
      , Text
    "swissgerman"
      , Text
    "italian"
      , Text
    "greek"
      , Text
    "azerbaijani"
      , Text
    "american"
      , Text
    "newzealand"
      , Text
    "UKenglish"
      , Text
    "USenglish"
      , Text
    "australian"
      , Text
    "british"
      , Text
    "canadian"
      , Text
    "english"
      , Text
    "bahasa"
      , Text
    "slovak"
      , Text
    "finnish"
      , Text
    "occitan"
      , Text
    "swedish"
      , Text
    "brazil"
      , Text
    "portuguese"
      , Text
    "portuges"
      , Text
    "brazilian"
      , Text
    "spanish"
      , Text
    "norwegian"
      , Text
    "norsk"
      , Text
    "nynorsk"
      , Text
    "bulgarian"
      , Text
    "breton"
      , Text
    "belarusian"
      , Text
    "piedmontese"
      , Text
    "esperanto"
      , Text
    "lithuanian"
      , Text
    "ukraineb"
      , Text
    "scottishgaelic"
      , Text
    "scottish"
      , Text
    "dutch"
      , Text
    "afrikaans"
      , Text
    "czech"
      , Text
    "serbian"
      , Text
    "latvian"
      , Text
    "catalan"
      , Text
    "basque"
      , Text
    "albanian"
      , Text
    "irish"
      , Text
    "serbianc"
      , Text
    "interlingua"
      , Text
    "bosnian"
      , Text
    "friulan"
      , Text
    "romanian"
      , Text
    "icelandic"
      , Text
    "classiclatin"
      , Text
    "ecclesiasticlatin"
      , Text
    "medievallatin"
      , Text
    "latin"
      , Text
    "georgian"
      , Text
    "macedonian"
      , Text
    "welsh"
      , Text
    "vietnamese"
      , Text
    "romansh"
      , Text
    "danish"
      , Text
    "lsorbian"
      , Text
    "usorbian"
      , Text
    "polish-compat"
      , Text
    "polish"
      , Text
    "estonian"
      , Text
    "french"
      , Text
    "frenchb"
      , Text
    "canadien"
      , Text
    "acadian"
      , Text
    "francais"
      , Text
    "turkish"
      , Text
    "hindi"
      , Text
    "northernsami"
      , Text
    "samin"
      , Text
    "russianb"
      , Text
    "galician"
      , Text
    "slovene"
      ]

    isMathEnv :: Text -> Bool
    isMathEnv :: Text -> Bool
    isMathEnv Text
    t =
      case Text -> Text -> Maybe Text
    T.stripPrefix Text
    "\\begin{" ((Char -> Bool) -> Text -> Text
    T.dropWhile Char -> Bool
    isSpaceChar Text
    t) of
        Maybe Text
    Nothing -> Bool
    False
        Just Text
    t' -> (Char -> Bool) -> Text -> Text
    T.takeWhile (Char -> Char -> Bool
    forall a. Eq a => a -> a -> Bool
    /= Char
    '}') Text
    t' Text -> [Text] -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem`
          [ Text
    "align", Text
    "align*"
          , Text
    "flalign", Text
    "flalign*"
          , Text
    "alignat", Text
    "alignat*"
          , Text
    "dmath", Text
    "dmath*"
          , Text
    "dgroup", Text
    "dgroup*"
          , Text
    "darray", Text
    "darray*"
          , Text
    "gather", Text
    "gather*"
          , Text
    "multline", Text
    "multline*"
          , Text
    "subequations"
          , Text
    "equation", Text
    "equation*"
          , Text
    "eqnarray"
          , Text
    "displaymath"
          ]
     where
       isSpaceChar :: Char -> Bool
    isSpaceChar Char
    '\n' = Bool
    True
       isSpaceChar Char
    '\r' = Bool
    True
       isSpaceChar Char
    '\t' = Bool
    True
       isSpaceChar Char
    ' ' = Bool
    True
       isSpaceChar Char
    _ = Bool
    False

    -- True if the math needs the cancel package
    needsCancel :: Text -> Bool
    needsCancel :: Text -> Bool
    needsCancel Text
    t =
      case Parser Bool -> Text -> Either String Bool
    forall a. Parser a -> Text -> Either String a
    A.parseOnly Parser Bool
    pCancel Text
    t of
        Right Bool
    True -> Bool
    True
        Either String Bool
    _ -> Bool
    False
     where
      pCancel :: Parser Bool
    pCancel = (Bool
    False Bool -> Parser Text () -> Parser Bool
    forall a b. a -> Parser Text b -> Parser Text a
    forall (f :: * -> *) a b. Functor f => a -> f b -> f a
    <$ Parser Text ()
    forall t. Chunk t => Parser t ()
    A.endOfInput) Parser Bool -> Parser Bool -> Parser Bool
    forall a. Parser Text a -> Parser Text a -> Parser Text a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> do
        c <- Parser Char
    A.anyChar
        case c of
          Char
    '\\' -> do
            x <- (Char -> Bool) -> Parser Text
    A.takeWhile Char -> Bool
    isLetter
            if x == "cancel" || x == "xcancel" || x == "bcancel"
               then return True
               else pCancel
          Char
    _ -> Parser Bool
    pCancel

    -- PDF standard support for DocumentMetadata

    -- | PDF standards supported by LaTeX's DocumentMetadata
    -- See: https://github.com/latex3/latex2e documentmetadata-support.dtx
    latexSupportedStandards :: Set.Set Text
    latexSupportedStandards :: Set Text
    latexSupportedStandards = [Text] -> Set Text
    forall a. Ord a => [a] -> Set a
    Set.fromList
      [ -- PDF/A standards (note: a-1a is NOT supported by LaTeX, only a-1b)
        Text
    "a-1b", Text
    "a-2a", Text
    "a-2b", Text
    "a-2u", Text
    "a-3a", Text
    "a-3b", Text
    "a-3u"
      , Text
    "a-4", Text
    "a-4e", Text
    "a-4f"
        -- PDF/X standards
      , Text
    "x-4", Text
    "x-4p", Text
    "x-5g", Text
    "x-5n", Text
    "x-5pg", Text
    "x-6", Text
    "x-6n", Text
    "x-6p"
        -- PDF/UA standards
      , Text
    "ua-1", Text
    "ua-2"
      ]

    -- | Standards that require PDF tagging (document structure)
    -- PDF/A level "a" variants and PDF/UA require tagged structure
    taggingRequiredStandards :: Set.Set Text
    taggingRequiredStandards :: Set Text
    taggingRequiredStandards = [Text] -> Set Text
    forall a. Ord a => [a] -> Set a
    Set.fromList
      [Text
    "a-2a", Text
    "a-3a", Text
    "ua-1", Text
    "ua-2"]

    -- | Valid PDF versions for DocumentMetadata
    validPdfVersions :: Set.Set Text
    validPdfVersions :: Set Text
    validPdfVersions = [Text] -> Set Text
    forall a. Ord a => [a] -> Set a
    Set.fromList
      [Text
    "1.4", Text
    "1.5", Text
    "1.6", Text
    "1.7", Text
    "2.0"]

    -- | PDF version required by each standard
    -- LaTeX defaults to PDF 2.0 with \DocumentMetadata, but some standards
    -- have maximum version requirements that are incompatible with 2.0
    standardRequiredVersion :: M.Map Text Text
    standardRequiredVersion :: Map Text Text
    standardRequiredVersion = [(Text, Text)] -> Map Text Text
    forall k a. Ord k => [(k, a)] -> Map k a
    M.fromList
      [ (Text
    "a-1b", Text
    "1.4")  -- PDF/A-1 requires exactly PDF 1.4
        -- PDF/A-2 and PDF/A-3 require 1.7; must set explicitly since LaTeX defaults to 2.0
      , (Text
    "a-2a", Text
    "1.7"), (Text
    "a-2b", Text
    "1.7"), (Text
    "a-2u", Text
    "1.7")
      , (Text
    "a-3a", Text
    "1.7"), (Text
    "a-3b", Text
    "1.7"), (Text
    "a-3u", Text
    "1.7")
        -- PDF/A-4, PDF/UA-1, PDF/UA-2 work with PDF 2.0 (the default)
      ]

    -- | Normalize a PDF standard string: lowercase, strip "pdf" prefix
    normalizePdfStandard :: Text -> Text
    normalizePdfStandard :: Text -> Text
    normalizePdfStandard Text
    t =
      let lower :: Text
    lower = Text -> Text
    T.toLower Text
    t
      in case Text -> Text -> Maybe Text
    T.stripPrefix Text
    "pdf" Text
    lower of
           Just Text
    rest -> (Char -> Bool) -> Text -> Text
    T.dropWhile (Char -> String -> Bool
    forall a. Eq a => a -> [a] -> Bool
    forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
    `elem` [Char
    '-', Char
    '/']) Text
    rest
           Maybe Text
    Nothing   -> Text
    lower

    -- | Normalize a PDF version string
    -- Handles YAML parsing quirk where 2.0 becomes integer 2, then string "2"
    normalizeVersion :: Text -> Text
    normalizeVersion :: Text -> Text
    normalizeVersion Text
    t
      | Text
    t Text -> Text -> Bool
    forall a. Eq a => a -> a -> Bool
    == Text
    "2" = Text
    "2.0"
      | Bool
    otherwise = Text
    t

    -- | Check if text is a valid PDF version (after normalization)
    isPdfVersion :: Text -> Bool
    isPdfVersion :: Text -> Bool
    isPdfVersion Text
    t = Text -> Set Text -> Bool
    forall a. Ord a => a -> Set a -> Bool
    Set.member (Text -> Text
    normalizeVersion Text
    t) Set Text
    validPdfVersions

    -- | Process pdfstandard metadata, returning PDF standard settings
    processPdfStandard :: PandocMonad m
                       => Context Text
                       -> m PdfStandard
    processPdfStandard :: forall (m :: * -> *).
    PandocMonad m =>
    Context Text -> m PdfStandard
    processPdfStandard Context Text
    ctx = do
      let standards :: [Text]
    standards = [Text] -> Maybe [Text] -> [Text]
    forall a. a -> Maybe a -> a
    fromMaybe [] (Maybe [Text] -> [Text]) -> Maybe [Text] -> [Text]
    forall a b. (a -> b) -> a -> b
    $ Text -> Context Text -> Maybe [Text]
    forall a b. FromContext a b => Text -> Context a -> Maybe b
    getField Text
    "pdfstandard" Context Text
    ctx
          normalized :: [Text]
    normalized = (Text -> Text) -> [Text] -> [Text]
    forall a b. (a -> b) -> [a] -> [b]
    map Text -> Text
    normalizePdfStandard [Text]
    standards
          ([Text]
    versions, [Text]
    pdfStds) = (Text -> Bool) -> [Text] -> ([Text], [Text])
    forall a. (a -> Bool) -> [a] -> ([a], [a])
    partition Text -> Bool
    isPdfVersion [Text]
    normalized
          validStandards :: [Text]
    validStandards = (Text -> Bool) -> [Text] -> [Text]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (Text -> Set Text -> Bool
    forall a. Ord a => a -> Set a -> Bool
    `Set.member` Set Text
    latexSupportedStandards) [Text]
    pdfStds
          invalidStandards :: [Text]
    invalidStandards = (Text -> Bool) -> [Text] -> [Text]
    forall a. (a -> Bool) -> [a] -> [a]
    filter (\Text
    s -> Bool -> Bool
    not (Text -> Set Text -> Bool
    forall a. Ord a => a -> Set a -> Bool
    Set.member Text
    s Set Text
    latexSupportedStandards)
                                        Bool -> Bool -> Bool
    && Bool -> Bool
    not (Text -> Bool
    isPdfVersion Text
    s)) [Text]
    pdfStds
          needsTagging :: Bool
    needsTagging = (Text -> Bool) -> [Text] -> Bool
    forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
    any (Text -> Set Text -> Bool
    forall a. Ord a => a -> Set a -> Bool
    `Set.member` Set Text
    taggingRequiredStandards) [Text]
    validStandards
          -- Use explicit version if provided, otherwise infer from standards
          -- Apply normalizeVersion to handle YAML parsing "2" -> "2.0"
          explicitVersion :: Maybe Text
    explicitVersion = Text -> Text
    normalizeVersion (Text -> Text) -> Maybe Text -> Maybe Text
    forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
    <$> [Text] -> Maybe Text
    forall a. [a] -> Maybe a
    listToMaybe [Text]
    versions
          inferredVersion :: Maybe Text
    inferredVersion = [Text] -> Maybe Text
    forall a. [a] -> Maybe a
    listToMaybe ([Text] -> Maybe Text) -> [Text] -> Maybe Text
    forall a b. (a -> b) -> a -> b
    $ (Text -> Maybe Text) -> [Text] -> [Text]
    forall a b. (a -> Maybe b) -> [a] -> [b]
    mapMaybe (Text -> Map Text Text -> Maybe Text
    forall k a. Ord k => k -> Map k a -> Maybe a
    `M.lookup` Map Text Text
    standardRequiredVersion) [Text]
    validStandards
          version :: Maybe Text
    version = Maybe Text
    explicitVersion Maybe Text -> Maybe Text -> Maybe Text
    forall a. Maybe a -> Maybe a -> Maybe a
    forall (f :: * -> *) a. Alternative f => f a -> f a -> f a
    <|> Maybe Text
    inferredVersion
      -- Warn about unsupported standards
      (Text -> m ()) -> [Text] -> m ()
    forall (t :: * -> *) (m :: * -> *) a b.
    (Foldable t, Monad m) =>
    (a -> m b) -> t a -> m ()
    mapM_ (LogMessage -> m ()
    forall (m :: * -> *). PandocMonad m => LogMessage -> m ()
    report (LogMessage -> m ()) -> (Text -> LogMessage) -> Text -> m ()
    forall b c a. (b -> c) -> (a -> b) -> a -> c
    . Text -> LogMessage
    UnsupportedPdfStandard) [Text]
    invalidStandards
      PdfStandard -> m PdfStandard
    forall a. a -> m a
    forall (m :: * -> *) a. Monad m => a -> m a
    return PdfStandard
        { pdfStandards :: [Text]
    pdfStandards = [Text]
    validStandards
        , pdfVersion :: Maybe Text
    pdfVersion = Maybe Text
    version
        , pdfTagging :: Bool
    pdfTagging = Bool
    needsTagging
        }