{- |
Module : Text.Highlighting.Kate.Format.LaTeX
Copyright : Copyright (C) 2008-2011 John MacFarlane
License : GNU GPL, version 2 or above
Maintainer : John MacFarlane <jgm@berkeley.edu>
Stability : alpha
Portability : portable
Formatters that convert a list of annotated source lines to LaTeX.
-}moduleText.Highlighting.Kate.Format.LaTeX(formatLaTeXInline,formatLaTeXBlock,styleToLaTeX)whereimportText.Highlighting.Kate.TypesimportText.PrintfimportData.List(intercalate)importControl.Monad(mplus)importData.Char(isSpace)formatLaTeX::FormatOptions->[SourceLine]->StringformatLaTeX_=intercalate"\n".mapsourceLineToLaTeX-- | Formats tokens as LaTeX using custom commands inside-- a @\\Highlight@ command. A @KeywordTok@ is rendered-- using @\\KeywordTok{..}@, and so on.formatLaTeXInline::FormatOptions->[SourceLine]->StringformatLaTeXInlineoptsls="\\Verb{"++formatLaTeXoptsls++"}"sourceLineToLaTeX::SourceLine->StringsourceLineToLaTeXcontents=concatMaptokenToLaTeXcontentstokenToLaTeX::Token->StringtokenToLaTeX(NormalTok,txt)|allisSpacetxt=escapeLaTeXtxttokenToLaTeX(toktype,txt)='\\':(showtoktype++"{"++escapeLaTeXtxt++"}")escapeLaTeX::String->StringescapeLaTeX=concatMapescapeLaTeXCharwhereescapeLaTeXChar'\\'="\\textbackslash{}"escapeLaTeXChar'{'="\\{"escapeLaTeXChar'}'="\\}"escapeLaTeXChar'|'="\\textbar{}"-- used in inline verbatimescapeLaTeXCharx=[x]-- LaTeX-- | Format tokens as a LaTeX @Highlighting@ environment inside a-- @Shaded@ environment. @Highlighting@ and @Shaded@ are-- defined by the macros produced by 'styleToLaTeX'. @Highlighting@-- is a verbatim environment using @fancyvrb@; @\\@, @{@, and @}@-- have their normal meanings inside this environment, so that-- formatting commands work. @Shaded@ is either nothing-- (if the style's background color is default) or a @snugshade@-- environment from @framed@, providing a background color-- for the whole code block, even if it spans multiple pages.formatLaTeXBlock::FormatOptions->[SourceLine]->StringformatLaTeXBlockoptsls=unlines["\\begin{Shaded}","\\begin{Highlighting}["++(ifnumberLinesoptsthen"numbers=left,"++(ifstartNumberopts==1then""else",firstnumber="++show(startNumberopts))++","else"")++"]",formatLaTeXoptsls,"\\end{Highlighting}","\\end{Shaded}"]-- | Converts a 'Style' to a set of LaTeX macro definitions,-- which should be placed in the document's preamble.-- Note: default LaTeX setup doesn't allow boldface typewriter font.-- To make boldface work in styles, you need to use a different typewriter-- font. This will work for computer modern:---- > \DeclareFontShape{OT1}{cmtt}{bx}{n}{<5><6><7><8><9><10><10.95><12><14.4><17.28><20.74><24.88>cmttb10}{}---- Or, with xelatex:---- > \usepackage{fontspec}-- > \setmainfont[SmallCapsFont={* Caps}]{Latin Modern Roman}-- > \setsansfont{Latin Modern Sans}-- > \setmonofont[SmallCapsFont={Latin Modern Mono Caps}]{Latin Modern Mono Light}--styleToLaTeX::Style->StringstyleToLaTeXf=unlines$["\\usepackage{color}","\\usepackage{fancyvrb}","\\DefineShortVerb[commandchars=\\\\\\{\\}]{\\|}","\\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\\\\{\\}}","% Add ',fontsize=\\small' for more characters per line"]++(casebackgroundColorfofNothing->["\\newenvironment{Shaded}{}{}"]Just(RGBrgb)->["\\usepackage{framed}",printf"\\definecolor{shadecolor}{RGB}{%d,%d,%d}"rgb,"\\newenvironment{Shaded}{\\begin{snugshade}}{\\end{snugshade}}"])++map(macrodef(defaultColorf)(tokenStylesf))(enumFromToKeywordTokNormalTok)macrodef::MaybeColor->[(TokenType,TokenStyle)]->TokenType->Stringmacrodefdefaultcoltokstylestokt="\\newcommand{\\"++showtokt++"}[1]{"++(co.ul.bf.it.bg$"{#1}")++"}"wheretokf=caselookuptokttokstylesofNothing->defStyleJustx->xulx=iftokenUnderlinetokfthen"\\underline{"++x++"}"elsexitx=iftokenItalictokfthen"\\textit{"++x++"}"elsexbfx=iftokenBoldtokfthen"\\textbf{"++x++"}"elsexbcol=fromColor`fmap`tokenBackgroundtokf::Maybe(Double,Double,Double)bgx=casebcolofNothing->xJust(r,g,b)->printf"\\colorbox[rgb]{%0.2f,%0.2f,%0.2f}{%s}"rgbxcol=fromColor`fmap`(tokenColortokf`mplus`defaultcol)::Maybe(Double,Double,Double)cox=casecolofNothing->xJust(r,g,b)->printf"\\textcolor[rgb]{%0.2f,%0.2f,%0.2f}{%s}"rgbx