8000 Add option to specify table env. in LaTeX output by pajowu · Pull Request #4392 · jgm/pandoc · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add option to specify table env. in LaTeX output #4392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions MANUAL.txt
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ Options affecting specific writers {.options}

: Use the [`listings`] package for LaTeX code blocks. The package
does not support multi-byte encoding for source code. To handle UTF-8
you would need to use a custom template. This issue is fully
you would need to use a custom template. This issue is fully
documented here: [Encoding issue with the listings package].

`-i`, `--incremental`
Expand Down Expand Up @@ -1148,10 +1148,18 @@ Options affecting specific writers {.options}
If used multiple times, the arguments are provided with spaces between
them. Note that no check for duplicate options is done.

`--latex-table-environment=longtable`|`tabularx`

: Use the specified table environment when producing LaTeX output.
The default is [`longtable`] which allows table to spread over multiple pages.
[`tabularx`] allows for paragraph-like columns whose width automatically expand to fill the width of the environment.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lines should be hard-wrapped to fit within 80 columns.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I quite understand this description: "paragraph-like columns whose width automatically expand to fill the width of the environment." Maybe better to say something about what should dictate this choice? For example: If you have long tables that may need to spans multiple pages, use longtable. If you want to create floating tables (and for better compatibility with other floating elements on the page), use tabularx.


[Dublin Core elements]: http://dublincore.org/documents/dces/
[ISO 8601 format]: http://www.w3.org/TR/NOTE-datetime
[Encoding issue with the listings package]:
https://en.wikibooks.org/wiki/LaTeX/Source_Code_Listings#Encoding_issue
[`longtable`]: https://ctan.org/pkg/longtable
[`tabularx`]: https://ctan.org/pkg/tabularx

Citation rendering {.options}
------------------
Expand Down Expand Up @@ -1599,6 +1607,10 @@ LaTeX variables are used when [creating a PDF].
supports 'plain' (default), 'empty', and 'headings'; headings puts
section titles in the header.

`table_environment`
: The environemnt given by `--latex-table-environment`
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"environment"



[`article`]: https://ctan.org/pkg/article
[`report`]: https://ctan.org/pkg/report
[`book`]: https://ctan.org/pkg/book
Expand Down Expand Up @@ -4484,17 +4496,17 @@ the [Beamer User's Guide] may also be used: `allowdisplaybreaks`,
Background in reveal.js and beamer
----------------------------------

Background images can be added to self-contained reveal.js slideshows and
Background images can be added to self-contained reveal.js slideshows and
to beamer slideshows.

For the same image on every slide, use the configuration
option `background-image` either in the YAML metadata block
or as a command-line variable. (There are no other options in
beamer and the rest of this section concerns reveal.js slideshows.)

For reveal.js, you can instead use the reveal.js-native option
`parallaxBackgroundImage`. You can also set `parallaxBackgroundHorizontal`
and `parallaxBackgroundVertical` the same way and must also set
For reveal.js, you can instead use the reveal.js-native option
`parallaxBackgroundImage`. You can also set `parallaxBackgroundHorizontal`
and `parallaxBackgroundVertical` the same way and must also set
`parallaxBackgroundSize` to have your values take effect.

To set an image for a particular reveal.js slide, add
Expand Down
2 changes: 1 addition & 1 deletion data/templates/default.latex
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ $if(highlighting-macros)$
$highlighting-macros$
$endif$
$if(tables)$
\usepackage{longtable,booktabs}
\usepackage{$table-environment$,booktabs}
$if(beamer)$
\usepackage{caption}
% These lines are needed to make table captions work with longtable:
Expand Down
11 changes: 11 additions & 0 deletions src/Text/Pandoc/App/CommandLineOptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,17 @@ options =
UTF8.hPutStr stdout (usageMessage prg options)
exitSuccess ))
"" -- "Show help"
, Option "" ["latex-table-class"]
Copy link
Owner
@jgm jgm Nov 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be latex-table-environment, given the documentation? (also elsewhere)

(ReqArg
(\arg opt ->
case toLower <$> arg of
"longtable" -> return opt { optLatexTableEnvironment = Longtable }
"tabularx" -> return opt { optLatexTableEnvironment = Tabularx }
-- mac-syntax (cr) is not supported in ghc-base.
_ -> E.throwIO $ PandocOptionError
"--latex-table-class must be longtable or tabularx")
"longtable|tabularx")
"" -- "EOL (default OS-dependent)"
]

getDataFileNames :: IO [FilePath]
Expand Down
6 changes: 5 additions & 1 deletion src/Text/Pandoc/App/Opt.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ import Text.Pandoc.Options (TopLevelDivision (TopLevelDefault),
WrapOption (WrapAuto), HTMLMathMethod (PlainMath),
ReferenceLocation (EndOfDocument),
ObfuscationMethod (NoObfuscation),
CiteMethod (Citeproc))
CiteMethod (Citeproc),
LatexTableEnvironment (Longtable))

#ifdef DERIVE_JSON_VIA_TH
import Data.Aeson.TH (deriveJSON, defaultOptions)
Expand Down Expand Up @@ -130,6 +131,7 @@ data Opt = Opt
, optRequestHeaders :: [(String, String)] -- ^ Headers for HTTP requests
, optEol :: LineEnding -- ^ Style of line-endings to use
, optStripComments :: Bool -- ^ Skip HTML comments
, optLatexTableEnvironment :: LatexTableEnvironment
} deriving (Generic, Show)

-- | Defaults for command-line options.
Expand Down Expand Up @@ -203,6 +205,8 @@ defaultOpts = Opt
, optRequestHeaders = []
, optEol = Native
, optStripComments = False
, optLatexTableEnvironment = Longtable

}

#ifdef DERIVE_JSON_VIA_TH
Expand Down
15 changes: 14 additions & 1 deletion src/Text/Pandoc/Options.hs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ module Text.Pandoc.Options ( module Text.Pandoc.Extensions
, WriterOptions (..)
, TrackChanges (..)
, ReferenceLocation (..)
, LatexTableEnvironment (..)
, def
, isEnabled
) where
Expand Down Expand Up @@ -168,6 +169,11 @@ data ReferenceLocation = EndOfBlock -- ^ End of block
| EndOfDocument -- ^ at end of document
deriving (Show, Read, Eq, Data, Typeable, Generic)

-- | Class of tables to use in LaTeX output
data LatexTableEnvironment = Longtable -- ^ Use longtable for tables
| Tabularx -- ^ Use tabularx tables
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should align as in the other examples in the code.

deriving (Show, Read, Eq, Data, Typeable, Generic)

-- | Options for writers
data WriterOptions = WriterOptions
{ writerTemplate :: Maybe String -- ^ Template to use
Expand Down Expand Up @@ -204,7 +210,8 @@ data WriterOptions = WriterOptions
, writerReferenceLocation :: ReferenceLocation -- ^ Location of footnotes and references for writing markdown
, writerSyntaxMap :: SyntaxMap
, writerPreferAscii :: Bool -- ^ Prefer ASCII representations of characters when possible
} deriving (Show, Data, Typeable, Generic)
, writerLatexTableEnvironment :: LatexTableEnvironment
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note clearly the API change in the commit comment.

} deriving (Show, Data, Typeable, Generic)

instance Default WriterOptions where
def = WriterOptions { writerTemplate = Nothing
Expand Down Expand Up @@ -239,6 +246,7 @@ instance Default WriterOptions where
, writerReferenceLocation = EndOfDocument
, writerSyntaxMap = defaultSyntaxMap
, writerPreferAscii = False
, writerLatexTableEnvironment = Longtable
}

instance HasSyntaxExtensions WriterOptions where
Expand All @@ -258,6 +266,7 @@ $(deriveJSON defaultOptions ''TrackChanges)
$(deriveJSON defaultOptions ''WrapOption)
$(deriveJSON defaultOptions ''TopLevelDivision)
$(deriveJSON defaultOptions ''ReferenceLocation)
$(deriveJSON defaultOptions ''LatexTableEnvironment)
#else
instance ToJSON CiteMethod where
toEncoding = genericToEncoding defaultOptions
Expand Down Expand Up @@ -294,4 +303,8 @@ instance FromJSON ReferenceLocation
instance ToJSON TrackChanges where
toEncoding = genericToEncoding defaultOptions
instance FromJSON TrackChanges

instance ToJSON LatexTableEnvironment where
toEncoding = genericToEncoding defaultOptions
instance FromJSON LatexTableEnvironment
#endif
38 changes: 30 additions & 8 deletions src/Text/Pandoc/Writers/LaTeX.hs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,8 @@ pandocToLaTeX options (Pandoc meta blocks) = do
else defField "dir" ("ltr" :: String)) $
defField "section-titles" True $
defField "geometry" geometryFromMargins $
defField "table-environment" (latexTableEnvironment
(writerLatexTableEnvironment options)) $
(case getField "papersize" metadata of
-- uppercase a4, a5, etc.
Just (('A':d:ds) :: String)
Expand Down Expand Up @@ -775,6 +777,7 @@ blockToLaTeX (Header level (id',classes,_) lst) = do
modify $ \s -> s{stInHeading = False}
return hdr
blockToLaTeX (Table caption aligns widths heads rows) = do
opts <- gets stOptions
(captionText, captForLof, footnotes) <- getCaption caption
let toHeaders hs = do contents <- tableRowToLaTeX True aligns widths hs
return ("\\toprule" $$ contents $$ "\\midrule")
Expand All @@ -794,20 +797,30 @@ blockToLaTeX (Table caption aligns widths heads rows) = do
else "\\caption" <> captForLof <> braces captionText
<> "\\tabularnewline"
rows' <- mapM (tableRowToLaTeX False aligns widths) rows
let colDescriptors = text $ concatMap toColDescriptor aligns
modify $ \s -> s{ stTable = True }
return $ "\\begin{longtable}[]" <>
braces ("@{}" <> colDescriptors <> "@{}")
-- the @{} removes extra space at beginning and end
return $ beginTable (writerLatexTableEnvironment opts) aligns
$$ capt
$$ firsthead
$$ head'
$$ "\\endhead"
$$ vcat rows'
$$ "\\bottomrule"
$$ "\\end{longtable}"
$$ endTable (writerLatexTableEnvironment opts)
$$ footnotes

beginTable :: LatexTableEnvironment -> [Alignment] -> Doc
beginTable tableEnvironment aligns = do
let colsWithIndices = case tableEnvironment of
Longtable -> map ((,) 0) aligns
Tabularx -> zip [0..] aligns
let colDescriptors = text $ concatMap toColDescriptor colsWithIndices
case tableEnvironment of
Longtable -> text "\\begin{longtable}[]" <>
braces ("@{}" <> colDescriptors <> "@{}")
Tabularx -> text "\\begin{tabularx}{\\linewidth}" <>
braces ("@{}" <> colDescriptors <> "@{}")
-- the @{} removes extra space at beginning and end

getCaption :: PandocMonad m => [Inline] -> LW m (Doc, Doc, Doc)
getCaption txt = do
inMinipage <- gets stInMinipage
Expand All @@ -822,13 +835,22 @@ getCaption txt = do
let footnotes = notesToLaTeX notes
return (capt, captForLof, footnotes)

toColDescriptor :: Alignment -> String
toColDescriptor align =
endTable :: LatexTableEnvironment -> Doc
endTable tableEnvironment = text "\\end{" <>
text (latexTableEnvironment tableEnvironment) <> "}"

toColDescriptor :: (Integer, Alignment) -> String
toColDescriptor (index, align) =
case align of
AlignLeft -> "l"
AlignRight -> "r"
AlignCenter -> "c"
AlignDefault -> "l"
AlignDefault -> if index == 0 then "l" else "X"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is "l" used for the first column and then "X"?


latexTableEnvironment :: LatexTableEnvironment -> String
latexTableEnvironment tableEnvironment = case tableEnvironment of
Longtable -> "longtable"
Tabularx -> "tabularx"

blockListToLaTeX :: PandocMonad m => [Block] -> LW m Doc
blockListToLaTeX lst =
Expand Down
0