module Text.LaTeX.Base.Commands
(
raw , between , comment , (%:)
, title
, author
, date
, institute
, thanks
, documentclass
, usepackage
, linespread
, article
, proc
, report
, minimal
, book
, slides
, ClassOption (..)
, customopt
, draft
, titlepage
, notitlepage
, onecolumn
, twocolumn
, oneside
, twoside
, landscape
, openright
, openany
, fleqn
, leqno
, PaperType (..)
, a0paper
, a1paper
, a2paper
, a3paper
, a4paper
, a5paper
, a6paper
, b0paper
, b1paper
, b2paper
, b3paper
, b4paper
, b5paper
, b6paper
, letterpaper
, executivepaper
, legalpaper
, pagestyle
, thispagestyle
, plain
, headings
, empty
, myheadings
, markboth
, markright
, document
, maketitle
, tableofcontents
, abstract
, appendix
, part
, chapter
, section
, subsection
, subsubsection
, paragraph
, subparagraph
, today
, thePage
, tex
, latex
, laTeX2
, laTeXe
, ldots
, vdots
, ddots
, hatex
, hatex3
, version
, hatex_version
, par
, newline
, lnbk
, lnbk_
, newpage
, cleardoublepage
, clearpage
, linebreak
, nolinebreak
, pagebreak
, nopagebreak
, hspace
, hspace_
, vspace
, hfill
, vfill
, stretch
, smallskip
, bigskip
, baselineskip
, indent
, noindent
, textwidth
, textheight
, linewidth
, verbatim , verb
, textbf
, textit
, texttt
, textrm
, textsf
, textmd
, textup
, textsl
, textsc
, textnormal
, underline
, emph
, tiny
, scriptsize
, footnotesize
, small
, normalsize
, large
, large2
, large3
, huge
, huge2
, enumerate
, itemize
, item
, flushleft
, flushright
, center
, quote
, verse
, cite
, description
, minipage
, figure
, pagenumbering
, arabic
, roman
, roman_
, alph
, alph_
, mbox
, fbox
, parbox
, framebox
, makebox
, raisebox
, rule
, caption
, label
, ref
, pageref
, tabular
, tabularnewline
, tabularnewlineSpc
, arraybackslash
, array
, (&)
, hline
, cline
, multicolumn
, matrixTabular
, centering
, raggedleft
, raggedright
, footnote
, protect
, hyphenation
, hyp
, qts
, input
, include
) where
import Data.String
import Data.Maybe (isNothing, catMaybes)
import Data.Text (toLower)
import qualified Data.Text as T
import Text.LaTeX.Base.Syntax
import Text.LaTeX.Base.Class
import Text.LaTeX.Base.Render
import Text.LaTeX.Base.Types
import Text.LaTeX.Base.Texy
import Data.Version
import Data.List (find, intercalate,intersperse)
import Data.Matrix (Matrix,nrows,ncols,(!))
import Paths_HaTeX
raw :: LaTeXC l => Text -> l
raw = fromLaTeX . TeXRaw
between :: Monoid m => m -> m -> m -> m
between c l1 l2 = l1 <> c <> l2
comment :: LaTeXC l => Text -> l
comment = fromLaTeX . TeXComment
(%:) :: LaTeXC l => l -> Text -> l
(%:) l = (l <>) . comment
maketitle :: LaTeXC l => l
maketitle = comm0 "maketitle"
title :: LaTeXC l => l -> l
title = comm1 "title"
date :: LaTeXC l => l -> l
date = comm1 "date"
author :: LaTeXC l => l -> l
author = comm1 "author"
institute :: LaTeXC l => Maybe l -> l -> l
institute Nothing = liftL $ \l -> TeXComm "institute" [FixArg l]
institute (Just s) = liftL2 (\l1 l2 -> TeXComm "institute" [OptArg l1,FixArg l2]) s
thanks :: LaTeXC l => l -> l
thanks = comm1 "thanks"
usepackage :: LaTeXC l => [l] -> PackageName -> l
usepackage ls pn = liftListL (\ls_ -> TeXComm "usepackage" [MOptArg ls_ ,FixArg $ fromString pn]) ls
latex :: LaTeXC l => l
latex = comm0 "LaTeX"
par :: LaTeXC l => l
par = comm0 "par"
newline :: LaTeXC l => l
newline = comm0 "newline"
part :: LaTeXC l => l -> l
part = comm1 "part"
chapter :: LaTeXC l => l -> l
chapter = comm1 "chapter"
section :: LaTeXC l => l -> l
section = comm1 "section"
subsection :: LaTeXC l => l -> l
subsection = comm1 "subsection"
subsubsection :: LaTeXC l => l -> l
subsubsection = comm1 "subsubsection"
paragraph :: LaTeXC l => l -> l
paragraph = comm1 "paragraph"
subparagraph :: LaTeXC l => l -> l
subparagraph = comm1 "subparagraph"
tableofcontents :: LaTeXC l => l
tableofcontents = comm0 "tableofcontents"
appendix :: LaTeXC l => l
appendix = comm0 "appendix"
item :: LaTeXC l => Maybe l -> l
item Nothing = commS "item "
item (Just opt) = liftL (\opt_ -> TeXComm "item" [OptArg opt_]) opt
enumerate :: LaTeXC l => l -> l
enumerate = liftL $ TeXEnv "enumerate" []
itemize :: LaTeXC l => l -> l
itemize = liftL $ TeXEnv "itemize" []
description :: LaTeXC l => l -> l
description = liftL $ TeXEnv "description" []
flushleft :: LaTeXC l => l -> l
flushleft = liftL $ TeXEnv "flushleft" []
flushright :: LaTeXC l => l -> l
flushright = liftL $ TeXEnv "flushright" []
center :: LaTeXC l => l -> l
center = liftL $ TeXEnv "center" []
quote :: LaTeXC l => l -> l
quote = liftL $ TeXEnv "quote" []
verse :: LaTeXC l => l -> l
verse = liftL $ TeXEnv "verse" []
minipage :: LaTeXC l =>
Maybe Pos
-> l
-> l
-> l
minipage Nothing = liftL2 $ \ts -> TeXEnv "minipage" [ FixArg ts ]
minipage (Just p) = liftL2 $ \ts -> TeXEnv "minipage" [ OptArg $ rendertex p , FixArg ts ]
figure :: LaTeXC l =>
Maybe Pos
-> l
-> l
figure Nothing = liftL $ TeXEnv "figure" []
figure (Just p) = liftL $ TeXEnv "figure" [ OptArg $ TeXRaw $ render p ]
abstract :: LaTeXC l => l -> l
abstract = liftL $ TeXEnv "abstract" []
cite :: LaTeXC l => l -> l
cite = liftL $ \l -> TeXComm "cite" [FixArg l]
data ClassOption =
Draft
| TitlePage
| NoTitlePage
| OneColumn
| TwoColumn
| OneSide
| TwoSide
| Landscape
| OpenRight
| OpenAny
| Fleqn
| Leqno
| FontSize Measure
| Paper PaperType
| CustomOption String
deriving Show
instance Render ClassOption where
render (FontSize m) = render m
render (Paper pt) = toLower (render pt) <> "paper"
render (CustomOption str) = fromString str
render co = toLower $ fromString $ show co
customopt :: String -> ClassOption
customopt = CustomOption
instance IsString ClassOption where
fromString = customopt
data PaperType =
A0 | A1 | A2 | A3 | A4 | A5 | A6
| B0 | B1 | B2 | B3 | B4 | B5 | B6
| Letter | Executive | Legal
deriving Show
instance Render PaperType where
documentclass :: LaTeXC l =>
[ClassOption]
-> ClassName
-> l
documentclass opts cn = fromLaTeX $ TeXComm "documentclass" [MOptArg $ fmap rendertex opts , FixArg $ fromString cn]
article :: ClassName
article = "article"
proc :: ClassName
proc = "proc"
minimal :: ClassName
minimal = "minimal"
report :: ClassName
report = "report"
book :: ClassName
book = "book"
slides :: ClassName
slides = "slides"
a0paper :: ClassOption
a0paper = Paper A0
a1paper :: ClassOption
a1paper = Paper A1
a2paper :: ClassOption
a2paper = Paper A2
a3paper :: ClassOption
a3paper = Paper A3
a4paper :: ClassOption
a4paper = Paper A4
a5paper :: ClassOption
a5paper = Paper A5
a6paper :: ClassOption
a6paper = Paper A6
b0paper :: ClassOption
b0paper = Paper B0
b1paper :: ClassOption
b1paper = Paper B1
b2paper :: ClassOption
b2paper = Paper B2
b3paper :: ClassOption
b3paper = Paper B3
b4paper :: ClassOption
b4paper = Paper B4
b5paper :: ClassOption
b5paper = Paper B5
b6paper :: ClassOption
b6paper = Paper B6
letterpaper :: ClassOption
letterpaper = Paper Letter
executivepaper :: ClassOption
executivepaper = Paper Executive
legalpaper :: ClassOption
legalpaper = Paper Legal
draft :: ClassOption
draft = Draft
fleqn :: ClassOption
fleqn = Fleqn
leqno :: ClassOption
leqno = Leqno
titlepage :: ClassOption
titlepage = TitlePage
notitlepage :: ClassOption
notitlepage = NoTitlePage
onecolumn :: ClassOption
onecolumn = OneColumn
twocolumn :: ClassOption
twocolumn = TwoColumn
oneside :: ClassOption
oneside = OneSide
twoside :: ClassOption
twoside = TwoSide
landscape :: ClassOption
landscape = Landscape
openright :: ClassOption
openright = OpenRight
openany :: ClassOption
openany = OpenAny
document :: LaTeXC l => l -> l
document = liftL $ TeXEnv "document" []
pagenumbering :: LaTeXC l => l -> l
pagenumbering = liftL $ \l -> TeXComm "pagenumbering" [FixArg l]
arabic :: LaTeXC l => l
arabic = fromLaTeX "arabic"
roman :: LaTeXC l => l
roman = fromLaTeX "roman"
roman_ :: LaTeXC l => l
roman_ = fromLaTeX "Roman"
alph :: LaTeXC l => l
alph = fromLaTeX "alph"
alph_ :: LaTeXC l => l
alph_ = fromLaTeX "Alph"
pagestyle :: LaTeXC l => PageStyle -> l
pagestyle ps = fromLaTeX $ TeXComm "pagestyle" [FixArg $ fromString ps]
thispagestyle :: LaTeXC l => PageStyle -> l
thispagestyle ps = fromLaTeX $ TeXComm "thispagestyle" [FixArg $ fromString ps]
plain :: PageStyle
plain = "plain"
headings :: PageStyle
headings = "headings"
empty :: PageStyle
empty = "empty"
myheadings :: PageStyle
myheadings = "myheadings"
markboth :: LaTeXC l => l -> l -> l
markboth = liftL2 $ \l1 l2 -> TeXComm "markboth" [FixArg l1 , FixArg l2]
markright :: LaTeXC l => l -> l
markright = liftL $ \l -> TeXComm "markright" [FixArg l]
lnbk :: LaTeXC l => l
lnbk = fromLaTeX $ TeXLineBreak Nothing False
lnbk_ :: LaTeXC l => l
lnbk_ = fromLaTeX $ TeXLineBreak Nothing True
hyp :: LaTeXC l => l
hyp = fromLaTeX $ TeXCommS "-"
cleardoublepage :: LaTeXC l => l
cleardoublepage = comm0 "cleardoublepage"
clearpage :: LaTeXC l => l
clearpage = comm0 "clearpage"
newpage :: LaTeXC l => l
newpage = comm0 "newpage"
linebreak :: LaTeXC l => l -> l
linebreak = liftL $ \l -> TeXComm "linebreak" [OptArg l]
nolinebreak :: LaTeXC l => l -> l
nolinebreak = liftL $ \l -> TeXComm "nolinebreak" [OptArg l]
nopagebreak :: LaTeXC l => l -> l
nopagebreak = liftL $ \l -> TeXComm "nopagebreak" [OptArg l]
pagebreak :: LaTeXC l => l -> l
pagebreak = liftL $ \l -> TeXComm "pagebreak" [OptArg l]
hyphenation :: LaTeXC l => l -> l
hyphenation = liftL $ \l -> TeXComm "hyphenation" [FixArg l]
mbox :: LaTeXC l => l -> l
mbox = liftL $ \l -> TeXComm "mbox" [FixArg l]
fbox :: LaTeXC l => l -> l
fbox = liftL $ \l -> TeXComm "fbox" [FixArg l]
today :: LaTeXC l => l
today = comm0 "today"
thePage :: LaTeXC l => l
thePage = comm0 "thepage"
tex :: LaTeXC l => l
tex = comm0 "TeX"
laTeX2 :: LaTeXC l => l
laTeX2 = comm0 "LaTeX"
laTeXe :: LaTeXC l => l
laTeXe = comm0 "LaTeXe"
ldots :: LaTeXC l => l
ldots = comm0 "ldots"
vdots :: LaTeXC l => l
vdots = comm0 "vdots"
ddots :: LaTeXC l => l
ddots = comm0 "ddots"
qts :: LaTeXC l => l -> l
qts l = between l (raw "``") (raw "''")
centering :: LaTeXC l => l
centering = comm0 "centering"
raggedleft :: LaTeXC l => l
raggedleft = comm0 "raggedleft"
raggedright :: LaTeXC l => l
raggedright = comm0 "raggedright"
footnote :: LaTeXC l => l -> l
footnote = liftL $ \l -> TeXComm "footnote" [FixArg l]
linespread :: LaTeXC l => Float -> l
linespread x = fromLaTeX $ TeXComm "linespread" [FixArg $ rendertex x]
baselineskip :: LaTeXC l => l
baselineskip = comm0 "baselineskip"
indent :: LaTeXC l => l
indent = comm0 "indent"
noindent :: LaTeXC l => l
noindent = comm0 "noindent"
hspace :: LaTeXC l => Measure -> l
hspace m = fromLaTeX $ TeXComm "hspace" [FixArg $ rendertex m]
hspace_ :: LaTeXC l => Measure -> l
hspace_ m = fromLaTeX $ TeXComm "hspace*" [FixArg $ rendertex m]
stretch :: LaTeXC l => Int -> l
stretch n = fromLaTeX $ TeXComm "stretch" [FixArg $ rendertex n]
vspace :: LaTeXC l => Measure -> l
vspace m = fromLaTeX $ TeXComm "vspace" [FixArg $ rendertex m]
hfill :: LaTeXC l => l
hfill = comm0 "hfill"
vfill :: LaTeXC l => l
vfill = comm0 "vfill"
protect :: LaTeXC l => l -> l
protect l = commS "protect" <> l
textwidth :: LaTeXC l => l
textwidth = comm0 "textwidth"
textheight :: LaTeXC l => l
textheight = comm0 "textheight"
linewidth :: LaTeXC l => l
linewidth = comm0 "linewidth"
verbatim :: LaTeXC l => Text -> l
verbatim = liftL (TeXEnv "verbatim" []) . raw
verb :: LaTeXC l => Text -> l
verb vbStr = case find (isNothing . (`T.find`vbStr) . (==))
$ "`'\"|=-~$#+/!^_;:,." ++ ['0'..'9'] ++ ['A'..'B'] ++ ['a'..'b']
of Just delim -> let d = T.singleton delim
in raw $ T.concat [ "\\verb", d, vbStr, d ]
Nothing -> let (lpart, rpart)
= T.splitAt (T.length vbStr `quot` 2) vbStr
in verb lpart <> verb rpart
underline :: LaTeXC l => l -> l
underline = liftL $ \l -> TeXComm "underline" [FixArg l]
emph :: LaTeXC l => l -> l
emph = liftL $ \l -> TeXComm "emph" [FixArg l]
textrm :: LaTeXC l => l -> l
textrm = liftL $ \l -> TeXComm "textrm" [FixArg l]
textsf :: LaTeXC l => l -> l
textsf = liftL $ \l -> TeXComm "textsf" [FixArg l]
texttt :: LaTeXC l => l -> l
texttt = liftL $ \l -> TeXComm "texttt" [FixArg l]
textmd :: LaTeXC l => l -> l
textmd = liftL $ \l -> TeXComm "textmd" [FixArg l]
textbf :: LaTeXC l => l -> l
textbf = liftL $ \l -> TeXComm "textbf" [FixArg l]
textup :: LaTeXC l => l -> l
textup = liftL $ \l -> TeXComm "textup" [FixArg l]
textit :: LaTeXC l => l -> l
textit = liftL $ \l -> TeXComm "textit" [FixArg l]
textsl :: LaTeXC l => l -> l
textsl = liftL $ \l -> TeXComm "textsl" [FixArg l]
textsc :: LaTeXC l => l -> l
textsc = liftL $ \l -> TeXComm "textsc" [FixArg l]
textnormal :: LaTeXC l => l -> l
textnormal = liftL $ \l -> TeXComm "textnormal" [FixArg l]
sizecomm :: LaTeXC l => String -> l -> l
sizecomm str = liftL $ \l -> TeXBraces $ comm0 str <> l
tiny :: LaTeXC l => l -> l
tiny = sizecomm "tiny"
scriptsize :: LaTeXC l => l -> l
scriptsize = sizecomm "scriptsize"
footnotesize :: LaTeXC l => l -> l
footnotesize = sizecomm "footnotesize"
small :: LaTeXC l => l -> l
small = sizecomm "small"
normalsize :: LaTeXC l => l -> l
normalsize = sizecomm "normalsize"
large :: LaTeXC l => l -> l
large = sizecomm "large"
large2 :: LaTeXC l => l -> l
large2 = sizecomm "Large"
large3 :: LaTeXC l => l -> l
large3 = sizecomm "LARGE"
huge :: LaTeXC l => l -> l
huge = sizecomm "huge"
huge2 :: LaTeXC l => l -> l
huge2 = sizecomm "Huge"
smallskip :: LaTeXC l => l
smallskip = comm0 "smallskip"
bigskip :: LaTeXC l => l
bigskip = comm0 "bigskip"
tabular :: LaTeXC l =>
Maybe Pos
-> [TableSpec]
-> l
-> l
tabular Nothing ts = liftL $ TeXEnv "tabular" [ FixArg $ TeXRaw $ renderAppend ts ]
tabular (Just p) ts = liftL $ TeXEnv "tabular" [ OptArg $ TeXRaw $ render p , FixArg $ TeXRaw $ renderAppend ts ]
array :: LaTeXC l =>
Maybe Pos
-> [TableSpec]
-> l
-> l
array Nothing ts = liftL $ TeXEnv "array" [ FixArg $ TeXRaw $ renderAppend ts ]
array (Just p) ts = liftL $ TeXEnv "array" [ OptArg $ TeXRaw $ render p , FixArg $ TeXRaw $ renderAppend ts ]
(&) :: LaTeXC l => l -> l -> l
(&) = between (raw "&")
hline :: LaTeXC l => l
hline = commS "hline "
tabularnewline :: LaTeXC l => l
tabularnewline = commS "tabularnewline "
tabularnewlineSpc :: LaTeXC l => Measure -> l
tabularnewlineSpc m = fromLaTeX $ TeXComm "tabularnewline" [OptArg $ rendertex m]
arraybackslash :: LaTeXC l => l
arraybackslash = commS "arraybackslash "
multicolumn :: LaTeXC l => Int -> [TableSpec] -> l -> l
multicolumn n c = liftL $ \l -> TeXComm "multicolumn"
[ FixArg $ rendertex n
, FixArg . TeXRaw $ renderAppend c
, FixArg l
]
matrixTabular :: (LaTeXC l, Texy a)
=> [l]
-> Matrix a
-> l
matrixTabular ts m =
let spec = VerticalLine : (intersperse VerticalLine $ replicate (ncols m) CenterColumn) ++ [VerticalLine]
in tabular Nothing spec $ mconcat
[ hline
, foldl1 (&) ts
, lnbk
, hline
, mconcat $ fmap (
\i -> mconcat [ foldl1 (&) $ fmap (\j -> texy (m ! (i,j))) [1 .. ncols m]
, lnbk
, hline
] ) [1 .. nrows m]
]
cline :: LaTeXC l => Int -> Int -> l
cline i j = fromLaTeX $ TeXComm "cline" [ FixArg $ TeXRaw $ render i <> "-" <> render j ]
parbox :: LaTeXC l => Maybe Pos -> Measure -> l -> l
parbox Nothing w = liftL $ \t -> TeXComm "parbox" [ FixArg $ rendertex w , FixArg t ]
parbox (Just p) w = liftL $ \t -> TeXComm "parbox" [ OptArg $ TeXRaw $ render p
, FixArg $ TeXRaw $ render w
, FixArg t ]
makebox :: LaTeXC l => Maybe Measure -> Maybe HPos -> l -> l
makebox Nothing Nothing = liftL $ \t -> TeXComm "makebox" [ FixArg t ]
makebox (Just w) Nothing = liftL $ \t -> TeXComm "makebox" [ OptArg $ TeXRaw $ render w
, FixArg t ]
makebox Nothing (Just p) = liftL $ \t -> TeXComm "makebox" [ OptArg $ TeXRaw $ render p
, FixArg t ]
makebox (Just w) (Just p) = liftL $ \t -> TeXComm "makebox" [ OptArg $ TeXRaw $ render w
, OptArg $ TeXRaw $ render p
, FixArg t ]
framebox :: LaTeXC l => Maybe Measure -> Maybe HPos -> l -> l
framebox Nothing Nothing = liftL $ \t -> TeXComm "framebox" [ FixArg t ]
framebox (Just w) Nothing = liftL $ \t -> TeXComm "framebox" [ OptArg $ TeXRaw $ render w
, FixArg t ]
framebox Nothing (Just p) = liftL $ \t -> TeXComm "framebox" [ OptArg $ TeXRaw $ render p
, FixArg t ]
framebox (Just w) (Just p) = liftL $ \t -> TeXComm "framebox" [ OptArg $ TeXRaw $ render w
, OptArg $ TeXRaw $ render p
, FixArg t ]
raisebox :: LaTeXC l => Measure -> Maybe Measure -> Maybe Measure -> l -> l
raisebox m ma mb = liftL $ \l -> TeXComm "raisebox" $
[ FixArg $ rendertex m ]
++ fmap (OptArg . rendertex) (catMaybes [ma,mb])
++ [ FixArg l ]
rule :: LaTeXC l =>
Maybe Measure
-> Measure
-> Measure
-> l
rule Nothing w h = fromLaTeX $ TeXComm "rule" [ FixArg $ TeXRaw $ render w
, FixArg $ TeXRaw $ render h ]
rule (Just l) w h = fromLaTeX $ TeXComm "rule" [ OptArg $ TeXRaw $ render l
, FixArg $ TeXRaw $ render w
, FixArg $ TeXRaw $ render h ]
hatex :: LaTeXC l => l
hatex = mbox $ "H"
<> hspace (Ex $ negate 0.3)
<> textsc "a"
<> hspace (Ex $ negate 0.3)
<> tex
hatex3 :: LaTeXC l => l
hatex3 = hatex <> emph (textbf "3")
hatex_version :: LaTeXC l => l
hatex_version = hatex
<> emph (textbf $ rendertex x)
<> hspace (Ex $ negate 0.3)
<> emph ("." <> fromString (intercalate "." $ fmap show xs))
where
(x:xs) = versionBranch version
caption :: LaTeXC l => l -> l
caption = liftL $ \l -> TeXComm "caption" [FixArg l]
label :: LaTeXC l => l -> l
label = liftL $ \l -> TeXComm "label" [FixArg $ TeXRaw $ render l]
ref :: LaTeXC l => l -> l
ref = liftL $ \l -> TeXComm "ref" [FixArg $ TeXRaw $ render l]
pageref :: LaTeXC l => l -> l
pageref = liftL $ \l -> TeXComm "pageref" [FixArg $ TeXRaw $ render l]
input :: LaTeXC l => FilePath -> l
input fp = fromLaTeX $ TeXComm "input" [FixArg $ TeXRaw $ fromString fp]
include :: LaTeXC l => FilePath -> l
include fp = fromLaTeX $ TeXComm "include" [FixArg $ TeXRaw $ fromString fp]