File: Latex.hs

package info (click to toggle)
haskell-tabular 0.2.2.8-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 104 kB
  • sloc: haskell: 307; makefile: 2
file content (53 lines) | stat: -rw-r--r-- 1,458 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module Text.Tabular.Latex where

import Data.List (intersperse)
import Text.Tabular


render :: (rh -> String)
       -> (ch -> String)
       -> (a -> String)
       -> Table rh ch a -> String
render = renderUsing (repeat "r")

renderUsing :: [String] -- ^ column header specifications including label (l,h,p{3cm},etc)
            -> (rh -> String)
            -> (ch -> String)
            -> (a -> String) -> Table rh ch a -> String
renderUsing cols fr fc f (Table rh ch cells) =
 unlines $ ( "\\begin{tabular}{" ++ hspec ++ "}")
         : [ addTableNl header
           , hline
           , (concatMap (either vAttr addTableNl) $
              flattenHeader $ fmap row $ zipHeader [] cells $ fmap fr rh)
           , "\\end{tabular}" ]
 where
  ch2 = Group DoubleLine [(Header ""),fmap fc ch]
  hspec  = concatMap (either hAttr fst) $ flattenHeader
         $ zipHeader "" cols ch2
  header = texCols . map label . headerContents $ ch2
  --
  row (cs,h) = texCols $ label h : map f cs
  texCols  = concat . intersperse " & "
  texRows  = map addTableNl
  rhStrings = headerContents rh


hline :: String
hline = "\\hline"

addTableNl :: String -> String
addTableNl = (++ "\\\\\n")

label :: String -> String
label s = "\\textbf{" ++ s ++ "}"

hAttr :: Properties -> String
hAttr NoLine     = ""
hAttr SingleLine = "|"
hAttr DoubleLine = "||"

vAttr :: Properties -> String
vAttr NoLine     = ""
vAttr SingleLine = hline
vAttr DoubleLine = hline ++ hline