HaTeX 3.4 → 3.5
raw patch · 18 files changed
+1004/−233 lines, 18 filesdep +attoparsecdep +matrixdep −parsecPVP ok
version bump matches the API change (PVP)
Dependencies added: attoparsec, matrix
Dependencies removed: parsec
API changes (from Hackage documentation)
- Text.LaTeX.Base: instance Fractional LaTeX
- Text.LaTeX.Base: instance Monad m => Eq (LaTeXT m a)
- Text.LaTeX.Base: instance Monad m => Fractional (LaTeXT m a)
- Text.LaTeX.Base: instance Monad m => Num (LaTeXT m a)
- Text.LaTeX.Base: instance Monad m => Show (LaTeXT m a)
- Text.LaTeX.Base: instance Num LaTeX
- Text.LaTeX.Base.Commands: hatex_meta :: LaTeXC l => l
- Text.LaTeX.Base.Parser: data ParseError :: *
- Text.LaTeX.Base.Parser: parseLaTeX :: Text -> Either ParseError LaTeX
- Text.LaTeX.Base.Syntax: TeXNewLine :: Bool -> LaTeX
- Text.LaTeX.Base.Types: instance Render Measure
- Text.LaTeX.Base.Types: instance Show Measure
- Text.LaTeX.Base.Writer: instance Functor m => Functor (LaTeXT m)
+ Text.LaTeX.Base.Parser: isMainDoc :: LaTeX -> Bool
+ Text.LaTeX.Base.Parser: latexAtOnce :: Text -> Either String LaTeX
+ Text.LaTeX.Base.Parser: latexBlockParser :: Parser LaTeX
+ Text.LaTeX.Base.Parser: latexDocParser :: Parser LaTeX
+ Text.LaTeX.Base.Parser: latexParser :: Parser LaTeX
+ Text.LaTeX.Base.Render: instance Render Measure
+ Text.LaTeX.Base.Render: instance Render Text
+ Text.LaTeX.Base.Syntax: Cm :: Double -> Measure
+ Text.LaTeX.Base.Syntax: CustomMeasure :: LaTeX -> Measure
+ Text.LaTeX.Base.Syntax: Dollar :: MathType
+ Text.LaTeX.Base.Syntax: Em :: Double -> Measure
+ Text.LaTeX.Base.Syntax: Ex :: Double -> Measure
+ Text.LaTeX.Base.Syntax: In :: Double -> Measure
+ Text.LaTeX.Base.Syntax: Mm :: Double -> Measure
+ Text.LaTeX.Base.Syntax: Parentheses :: MathType
+ Text.LaTeX.Base.Syntax: Pt :: Int -> Measure
+ Text.LaTeX.Base.Syntax: Square :: MathType
+ Text.LaTeX.Base.Syntax: TeXLineBreak :: (Maybe Measure) -> Bool -> LaTeX
+ Text.LaTeX.Base.Syntax: data MathType
+ Text.LaTeX.Base.Syntax: data Measure
+ Text.LaTeX.Base.Syntax: instance Eq MathType
+ Text.LaTeX.Base.Syntax: instance Eq Measure
+ Text.LaTeX.Base.Syntax: instance Ord MathType
+ Text.LaTeX.Base.Syntax: instance Show MathType
+ Text.LaTeX.Base.Syntax: instance Show Measure
+ Text.LaTeX.Base.Types: HCenter :: HPos
+ Text.LaTeX.Base.Types: HLeft :: HPos
+ Text.LaTeX.Base.Types: HRight :: HPos
+ Text.LaTeX.Base.Types: data HPos
+ Text.LaTeX.Base.Types: instance Render HPos
+ Text.LaTeX.Base.Types: instance Show HPos
+ Text.LaTeX.Base.Writer: instance Applicative f => Applicative (LaTeXT f)
+ Text.LaTeX.Base.Writer: instance Functor f => Functor (LaTeXT f)
+ Text.LaTeX.Packages.AMSMath: (*:) :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: arccot :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: autoAngleBrackets :: LaTeXC l => l -> l
+ Text.LaTeX.Packages.AMSMath: autoBraces :: LaTeXC l => l -> l
+ Text.LaTeX.Packages.AMSMath: autoBrackets :: LaTeXC l => LaTeX -> LaTeX -> l -> l
+ Text.LaTeX.Packages.AMSMath: autoParens :: LaTeXC l => l -> l
+ Text.LaTeX.Packages.AMSMath: autoSquareBrackets :: LaTeXC l => l -> l
+ Text.LaTeX.Packages.AMSMath: b2matrix :: (Render a, LaTeXC l) => Maybe HPos -> Matrix a -> l
+ Text.LaTeX.Packages.AMSMath: bmatrix :: (Render a, LaTeXC l) => Maybe HPos -> Matrix a -> l
+ Text.LaTeX.Packages.AMSMath: bullet :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: cap :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: cdot :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: circ :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: cot :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: coth :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: csc :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: cup :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: dblPipe :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: div_ :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: equiv :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: exists :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: frac :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: gg :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: instance Floating LaTeX
+ Text.LaTeX.Packages.AMSMath: instance Fractional LaTeX
+ Text.LaTeX.Packages.AMSMath: instance Monad m => Eq (LaTeXT m a)
+ Text.LaTeX.Packages.AMSMath: instance Monad m => Floating (LaTeXT m a)
+ Text.LaTeX.Packages.AMSMath: instance Monad m => Fractional (LaTeXT m a)
+ Text.LaTeX.Packages.AMSMath: instance Monad m => Num (LaTeXT m a)
+ Text.LaTeX.Packages.AMSMath: instance Monad m => Show (LaTeXT m a)
+ Text.LaTeX.Packages.AMSMath: instance Num LaTeX
+ Text.LaTeX.Packages.AMSMath: langle :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: lceil :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: lfloor :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: ll :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: mapsto :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: mathDisplay :: LaTeXC l => l -> l
+ Text.LaTeX.Packages.AMSMath: mathdefault :: LaTeXC l => l -> l
+ Text.LaTeX.Packages.AMSMath: mp :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: odot :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: ominus :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: oplus :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: oslash :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: otimes :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: pm :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: pmatrix :: (Render a, LaTeXC l) => Maybe HPos -> Matrix a -> l
+ Text.LaTeX.Packages.AMSMath: propto :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: rangle :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: rceil :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: rfloor :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: sec :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: setminus :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: star :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: subset :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: supset :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: tcosh :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: times :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: tsinh :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: tsqrt :: LaTeXC l => Maybe l -> l -> l
+ Text.LaTeX.Packages.AMSMath: ttanh :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: v2matrix :: (Render a, LaTeXC l) => Maybe HPos -> Matrix a -> l
+ Text.LaTeX.Packages.AMSMath: vartheta :: LaTeXC l => l
+ Text.LaTeX.Packages.AMSMath: vee :: LaTeXC l => l -> l -> l
+ Text.LaTeX.Packages.AMSMath: vmatrix :: (Render a, LaTeXC l) => Maybe HPos -> Matrix a -> l
+ Text.LaTeX.Packages.AMSMath: wedge :: LaTeXC l => l -> l -> l
- Text.LaTeX.Base.Commands: thispagestyle :: LaTeX -> LaTeX
+ Text.LaTeX.Base.Commands: thispagestyle :: LaTeXC l => l -> l
- Text.LaTeX.Base.Syntax: TeXMath :: LaTeX -> LaTeX
+ Text.LaTeX.Base.Syntax: TeXMath :: MathType -> LaTeX -> LaTeX
- Text.LaTeX.Base.Types: Cm :: Float -> Measure
+ Text.LaTeX.Base.Types: Cm :: Double -> Measure
- Text.LaTeX.Base.Types: Em :: Float -> Measure
+ Text.LaTeX.Base.Types: Em :: Double -> Measure
- Text.LaTeX.Base.Types: Ex :: Float -> Measure
+ Text.LaTeX.Base.Types: Ex :: Double -> Measure
- Text.LaTeX.Base.Types: In :: Float -> Measure
+ Text.LaTeX.Base.Types: In :: Double -> Measure
- Text.LaTeX.Base.Types: Mm :: Float -> Measure
+ Text.LaTeX.Base.Types: Mm :: Double -> Measure
Files
- Examples/beamer.hs +38/−0
- Examples/matrices.hs +22/−0
- Examples/simple.hs +8/−5
- HaTeX.cabal +10/−9
- README.md +8/−4
- Text/LaTeX/Base.hs +1/−45
- Text/LaTeX/Base/Class.hs +8/−0
- Text/LaTeX/Base/Commands.hs +3/−8
- Text/LaTeX/Base/Parser.hs +302/−81
- Text/LaTeX/Base/Render.hs +63/−25
- Text/LaTeX/Base/Syntax.hs +26/−3
- Text/LaTeX/Base/Types.hs +13/−22
- Text/LaTeX/Base/Warnings.hs +1/−1
- Text/LaTeX/Base/Writer.hs +12/−9
- Text/LaTeX/Packages/AMSMath.hs +481/−17
- Text/LaTeX/Packages/Beamer.hs +5/−1
- Text/LaTeX/Packages/Graphicx.hs +2/−2
- license +1/−1
+ Examples/beamer.hs view
@@ -0,0 +1,38 @@+ +{-# LANGUAGE OverloadedStrings #-} + +import Text.LaTeX +import Text.LaTeX.Packages.Beamer +import Text.LaTeX.Packages.Inputenc + +main :: IO () +main = execLaTeXT beamerExample >>= renderFile "beamer.tex" + +beamerExample :: Monad m => LaTeXT_ m +beamerExample = thePreamble >> document theBody + +thePreamble :: Monad m => LaTeXT_ m +thePreamble = do + -- To start using beamer, set the document class to be 'beamer'. + documentclass [] beamer + usepackage [utf8] inputenc + author "Daniel Díaz" + title "Beamer example using HaTeX" + -- Use a theme to improve the aspect of your slides. + usetheme CambridgeUS + +theBody :: Monad m => LaTeXT_ m +theBody = do + -- Each slide is a call to the function 'frame' with the content of the slide. + frame maketitle + frame $ do + frametitle "Example with block" + "Here comes " + alert [FromSlide 2] "the block!" + uncover [FromSlide 5] " Ha! I was hiding here!" + pause + pause + block "The block" $ do + pause + "The content of the block appears after a pause." + uncover [FromSlide 6] $ center "And that's it!"
+ Examples/matrices.hs view
@@ -0,0 +1,22 @@+ +import Text.LaTeX +import Text.LaTeX.Packages.AMSMath + +import Data.Matrix + +main :: IO () +main = execLaTeXT matrices >>= renderFile "matrices.tex" + +matrices :: Monad m => LaTeXT_ m +matrices = do + thePreamble + document theBody + +thePreamble :: Monad m => LaTeXT_ m +thePreamble = do + documentclass [] article + -- You need to import 'amsmath' to use 'pmatrix'. + usepackage [] amsmath + +theBody :: Monad m => LaTeXT_ m +theBody = equation_ $ pmatrix Nothing $ (identity 5 :: Matrix Int)
Examples/simple.hs view
@@ -1,10 +1,10 @@ {- Simple example -This example is intended to be simplest as possible, but containing the most significant parts. +This example is intended to be as simple as possible, but containing the most significant parts. -The Overloaded Strings language extension is quite handy, because it allows you to write text without -using 'fromString' each time. +The Overloaded Strings language extension is quite handy because it allows you to write text without +using 'fromString' everywhere. -} @@ -38,5 +38,8 @@ "This is a simple example using the " hatex " library. " - -- 'textbf' turns characters to bold font. - textbf "Enjoy!"+ -- 'textbf' turns characters to bold font (as you already may know). + textbf "Enjoy!" + " " + -- This is how we nest commands. + textbf (large "Yoohoo!")
HaTeX.cabal view
@@ -1,5 +1,5 @@ Name: HaTeX -Version: 3.4 +Version: 3.5 Author: Daniel Díaz Category: Text Build-type: Simple @@ -7,9 +7,9 @@ License-file: license Maintainer: Daniel Díaz (dhelta `dot` diaz `at` gmail `dot` com) Stability: Experimental -Bug-reports: dhelta `dot` diaz `at` gmail `dot` com +Bug-reports: https://github.com/Daniel-Diaz/HaTeX/issues Synopsis: The Haskell LaTeX library. -Description: As it is said: \"LaTeX is a high-quality typesetting system\". This library provides +Description: The LaTeX project site states: \"LaTeX is a high-quality typesetting system\". This library provides a bridge between that system and Haskell (i.e. it is a LaTeX DSL). . Write LaTeX documents with all the advantages you already have in Haskell (recursion, @@ -18,10 +18,9 @@ to pretty-printing values, ... . See the @examples@ directory in the source distribution to look some simple examples. - It would be good to get you started. Download the HaTeX User's Guide from - <https://github.com/downloads/Daniel-Diaz/HaTeX-Guide/HaTeX-Guide.pdf>. + It would be good to get you started. HaTeX User's Guide is available at + <https://github.com/Daniel-Diaz/HaTeX-Guide>. Cabal-version: >= 1.6 -Tested-with: GHC == 7.0.3 , GHC == 7.4.1 Extra-source-files: ReleaseNotes README.md @@ -30,6 +29,8 @@ Examples/comments.hs Examples/simple.hs Examples/tree.hs + Examples/beamer.hs + Examples/matrices.hs Source-repository head type: git @@ -40,7 +41,8 @@ , bytestring , transformers >= 0.2.2 && < 0.4 , text - , parsec >=3.1.2 && <3.2 + , attoparsec + , matrix Exposed-modules: Text.LaTeX -- Base (Core of the library) @@ -67,6 +69,5 @@ Text.LaTeX.Packages.Trees Text.LaTeX.Packages.Trees.Qtree Other-modules: Paths_HaTeX - Extensions: GeneralizedNewtypeDeriving - , OverloadedStrings + Extensions: OverloadedStrings , CPP
README.md view
@@ -1,19 +1,23 @@-# HaTeX README +# HaTeX ReadMe `HaTeX` is the LaTeX syntax implementation done in Haskell. +Check a list of examples of usage in the [examples](Examples/) directory. +A good starting point may be [simple.hs](Examples/simple.hs). + ## TODO list * Add more examples. * More testing on the parser. * Add more documentation. -* A more complete `AMSMath` module. +* A more complete `AMSMath` module. Split environment equations. +* Implement more packages (fontenc, geometry, babel, array, ...). +* BibTeX support. ## Installation notes * To install `HaTeX`, use the typical `cabal install`. -* Eventually you may require to install `mtl` first (there is a problem with the `transformers` dependency). ## HaTeX User's Guide -* The HaTeX User's Guide lives [here](https://github.com/Daniel-Diaz/HaTeX-Guide).+* The HaTeX User's Guide lives [here](https://github.com/Daniel-Diaz/HaTeX-Guide)... and is also done in Haskell!
Text/LaTeX/Base.hs view
@@ -18,8 +18,6 @@ and environments. * The "Text.LaTeX.Base.Writer" module, to work with the monad interface of the library. - -Here are also defined 'Num' and 'Fractional' instances for both 'LaTeX' and 'LaTeXT'. -} module Text.LaTeX.Base ( -- * @LaTeX@ datatype @@ -48,46 +46,4 @@ import Text.LaTeX.Base.Commands import Text.LaTeX.Base.Writer -- -import Data.Monoid - --- Num and Fractional instances for LaTeX and LaTeXT - --- | Careful! Method 'signum' is undefined. Don't use it! -instance Num LaTeX where - (+) = TeXOp "+" - (-) = TeXOp "-" - (*) = (<>) - negate = (TeXEmpty -) - fromInteger = rendertex - abs x = between x "|" "|" - -- Non-defined methods - signum _ = error "Cannot use \"signum\" Num method with a LaTeX value." - --- | Division uses the LaTeX @frac@ command. -instance Fractional LaTeX where - p / q = TeXComm "frac" [FixArg p, FixArg q] - fromRational = rendertex . (fromRational :: Rational -> Float) - --- | Warning: this instance only exist for the 'Num' instance. -instance Monad m => Eq (LaTeXT m a) where - _ == _ = error "Cannot use \"(==)\" Eq method with a LaTeXT value." - --- | Warning: this instance only exist for the 'Num' instance. -instance Monad m => Show (LaTeXT m a) where - show _ = error "Cannot use \"show\" Show method with a LaTeXT value." - --- | Careful! Method 'signum' is undefined. Don't use it! -instance Monad m => Num (LaTeXT m a) where - (+) = liftOp (+) - (-) = liftOp (-) - (*) = (>>) - negate = (mempty -) - fromInteger = fromLaTeX . fromInteger - abs = liftL abs - -- Non-defined methods - signum _ = error "Cannot use \"signum\" Num method with a LaTeXT value." - --- | Division uses the LaTeX @frac@ command. -instance Monad m => Fractional (LaTeXT m a) where - (/) = liftOp (/) - fromRational = fromLaTeX . fromRational+import Data.Monoid
Text/LaTeX/Base/Class.hs view
@@ -6,10 +6,18 @@ LaTeXC (..) , Monoid (..) -- * Combinators + -- ** From @LaTeX@ , fromLaTeX + -- ** Lifting + -- | Lifting functions from 'LaTeX' functions to functions over any instance of 'LaTeXC'. + -- In general, the implementation is as follows: + -- + -- > liftLN f x1 ... xN = liftListL (\[x1,...,xN] -> f x1 ... xN) [x1,...,xN] + -- , liftL , liftL2 , liftL3 + -- ** Others , comm0 , commS , braces
Text/LaTeX/Base/Commands.hs view
@@ -91,7 +91,6 @@ -- *** HaTeX specific , hatex , hatex3 - , hatex_meta , version , hatex_version -- ** Document layout @@ -553,7 +552,7 @@ pagestyle :: LaTeXC l => l -> l pagestyle = liftL $ \l -> TeXComm "pagestyle" [FixArg l] -thispagestyle :: LaTeX -> LaTeX +thispagestyle :: LaTeXC l => l -> l thispagestyle = liftL $ \l -> TeXComm "thispagestyle" [FixArg l] plain :: LaTeXC l => l @@ -578,10 +577,10 @@ -- | Start a new line. In a 'tabular', it starts a new row, so use 'newline' instead. lnbk :: LaTeXC l => l -lnbk = fromLaTeX $ TeXNewLine False +lnbk = fromLaTeX $ TeXLineBreak Nothing False lnbk_ :: LaTeXC l => l -lnbk_ = fromLaTeX $ TeXNewLine True +lnbk_ = fromLaTeX $ TeXLineBreak Nothing True hyp :: LaTeXC l => l hyp = fromLaTeX $ TeXCommS "-" @@ -843,10 +842,6 @@ -- | Print the HaTeX 3 logo. hatex3 :: LaTeXC l => l hatex3 = hatex <> emph (textbf "3") - --- | Print the HaTeX-meta logo. -hatex_meta :: LaTeXC l => l -hatex_meta = hatex <> emph (textsc "-meta") -- | Print the HaTeX logo, beside the complete version number. hatex_version :: LaTeXC l => l
Text/LaTeX/Base/Parser.hs view
@@ -1,105 +1,326 @@+{-# LANGUAGE CPP #-} +------------------------------------------------------------------------------- +-- | +-- Module : Text/LaTeX/Base/Parser.hs +-- Copyright : (c) Tobias Schoofs +-- License : LGPL +-- Stability : experimental +-- Portability: portable +-- +-- LaTeX Parser based on Attoparsec +------------------------------------------------------------------------------- +module Text.LaTeX.Base.Parser ( + latexParser, + latexBlockParser, + latexAtOnce, + latexDocParser, + isMainDoc +#ifdef _TEST + , specials +#endif + ) +where --- | This is the 'LaTeX' parser module. -module Text.LaTeX.Base.Parser - ( parseLaTeX - , ParseError (..) - ) where + import Data.Attoparsec.Text hiding (take, takeTill) + import qualified Data.Attoparsec.Text as A (takeTill) + import Data.Char (toLower) + import Data.Monoid + import Data.Text (Text) + import qualified Data.Text as T -import Text.LaTeX.Base.Syntax --- -import Text.Parsec -import Text.Parsec.Text --- -import Control.Applicative hiding ((<|>),many) -import Data.Monoid -import Data.String -import Data.Text + import Control.Applicative ((<|>), (<$>)) + import Control.Monad (unless) -p_Comm :: Parser LaTeX -p_Comm = do - char '\\' - str <- many1 alphaNum - args <- try (string "{}" >> return []) - <|> (many1 p_Arg) - return $ TeXComm str args + import Text.LaTeX.Base.Syntax -p_CommS :: Parser LaTeX -p_CommS = do - char '\\' - TeXCommS <$> many1 alphaNum + ------------------------------------------------------------------------ + -- | Parses a Text sequence at once; + -- may fail or conclude. + ------------------------------------------------------------------------ + latexAtOnce :: Text -> Either String LaTeX + latexAtOnce t | T.null t = return TeXEmpty + | otherwise = + case parse latexParser t of + Fail _ _ e -> Left e + Done _ r -> Right r + rx@(Partial _) -> -- Left "incomplete input" + case feed rx T.empty of + Fail _ _ e -> Left e + Partial _ -> Left "incomplete input" + Done _ r -> Right r -p_Env :: Parser LaTeX -p_Env = do - string "\\begin{" - str <- many1 alphaNum - char '}' - args <- many p_Arg - ls <- manyTill p_TeXU $ try $ string $ "\\end{" ++ str ++ "}" - return $ TeXEnv str args $ mconcat ls + ------------------------------------------------------------------------ + -- | The incremental LaTeX Parser + ------------------------------------------------------------------------ + latexParser :: Parser LaTeX + latexParser = blocks -p_Math :: Parser LaTeX -p_Math = do - char '$' - TeXMath . mconcat <$> manyTill p_TeXU (char '$') + ------------------------------------------------------------------------ + -- | Incremental Parser for single blocks of LaTeX + ------------------------------------------------------------------------ + latexBlockParser :: Parser LaTeX + latexBlockParser = block -p_NewLine :: Parser LaTeX -p_NewLine = do - string "\\\\" - fmap TeXNewLine $ (char '*' >> return True) <|> return False + ------------------------------------------------------------------------ + -- | Incremental Parser that terminates after the /document/ envionment + ------------------------------------------------------------------------ + latexDocParser :: Parser LaTeX + latexDocParser = blockTillDoc -p_RawG :: [Char] -> Parser LaTeX -p_RawG xs = TeXRaw . fromString <$> manyTill anyChar (lookAhead $ (oneOf xs >> return mempty) <|> p_TeXURaw) + ------------------------------------------------------------------------ + -- Blocks + ------------------------------------------------------------------------ + blocks :: Parser LaTeX + blocks = mconcat <$> block `manyTill` endOfInput -p_Raw :: Parser LaTeX -p_Raw = p_RawG [] + blockTillDoc :: Parser LaTeX + blockTillDoc = do + b <- block + if isMainDoc b then return b + else mappend b <$> blockTillDoc -p_RawNC :: Parser LaTeX -p_RawNC = p_RawG "," + -- | Test if a 'LaTeX' block is a @document@ environment. + isMainDoc :: LaTeX -> Bool + isMainDoc b = case b of + TeXEnv "document" _ _ -> True + _ -> False -p_TeXU :: Parser LaTeX -p_TeXU = choice $ try <$> [p_NewLine , p_Math , p_Env , p_Comm , p_CommS , p_Raw] + ------------------------------------------------------------------------ + -- Block + -- Note: text stops on ']'; + -- if the other parser fail on the rest + -- text2 handles it, starting with ']' + ------------------------------------------------------------------------ + block :: Parser LaTeX + block = choice [text, dolMath, comment, environment, command, text2] + + ------------------------------------------------------------------------ + -- Text + ------------------------------------------------------------------------ + text :: Parser LaTeX + text = do + mbC <- peekChar + case mbC of + Nothing -> return TeXEmpty + Just c | c `elem` "$%\\{]}" -> fail "not text" + | otherwise -> TeXRaw <$> A.takeTill (`elem` "$%\\{]}") -p_TeXURaw :: Parser LaTeX -p_TeXURaw = choice $ try <$> [p_NewLine , p_Math , p_Env , p_Comm , p_CommS] + ------------------------------------------------------------------------ + -- Text without stopping on ']' + ------------------------------------------------------------------------ + text2 :: Parser LaTeX + text2 = do + _ <- char ']' + t <- try (text <|> return (TeXRaw T.empty)) + return $ TeXRaw endlessSq <> t -p_TeX :: Parser LaTeX -p_TeX = mconcat <$> many p_TeXU + ------------------------------------------------------------------------ + -- Environment + ------------------------------------------------------------------------ + environment :: Parser LaTeX + environment = choice [anonym, env] -p_TeXUNC :: Parser LaTeX -p_TeXUNC = choice $ try <$> [p_NewLine , p_Math , p_Env , p_Comm , p_CommS , p_RawNC] + anonym :: Parser LaTeX + anonym = char oBr >> + TeXBraces . mconcat <$> block `manyTill` char eBr -p_TeXNC :: Parser LaTeX -p_TeXNC = mconcat <$> many p_TeXUNC + env :: Parser LaTeX + env = do + n <- envName begin + as <- cmdArgs + b <- envBody n + return $ TeXEnv (T.unpack n) as b -p_SimpleArg :: (Char,Char) -> (LaTeX -> TeXArg) -> Parser TeXArg -p_SimpleArg (c1,c2) f = do - char c1 - fmap (f . mconcat) $ manyTill (p_TeXURaw <|> p_RawG [c2]) $ char c2 + envName :: Text -> Parser Text + envName k = do + _ <- string k + _ <- char oBr + n <- A.takeTill (== eBr) + _ <- char eBr + return n -p_MultiArg :: (Char,Char) -> ([LaTeX] -> TeXArg) -> Parser TeXArg -p_MultiArg (c1,c2) f = do - char c1 - fmap f $ sepBy1 (mconcat <$> manyTill p_TeXUNC (char c2)) $ char ',' + envBody :: Text -> Parser LaTeX + envBody n = mconcat <$> block `manyTill` endenv + where endenv = try $ string (end `T.snoc` oBr <> n + `T.snoc` eBr) -p_Opt :: Parser TeXArg -p_Opt = p_SimpleArg ('[',']') OptArg + ------------------------------------------------------------------------ + -- Command + ------------------------------------------------------------------------ + command :: Parser LaTeX + command = do + _ <- char bsl + mbX <- peekChar + case mbX of + Nothing -> return TeXEmpty + Just x -> if isSpecial x + then special + else do + c <- A.takeTill endCmd + as <- cmdArgs + if null as + then return $ TeXCommS (T.unpack c) + else return $ TeXComm (T.unpack c) as -p_MOpt :: Parser TeXArg -p_MOpt = p_MultiArg ('[',']') MOptArg + ------------------------------------------------------------------------ + -- Command Arguments + ------------------------------------------------------------------------ + cmdArgs :: Parser [TeXArg] + cmdArgs = try (whitespace >> string emptyArg >> return [FixArg TeXEmpty]) + <|> many1 cmdArg + <|> return [] -p_Fix :: Parser TeXArg -p_Fix = p_SimpleArg ('{','}') FixArg + cmdArg :: Parser TeXArg + cmdArg = do + whitespace + c <- char '[' <|> char '{' + let e = case c of + '[' -> endlessSq + '{' -> endlessBr + _ -> error "this cannot happen!" + b <- mconcat <$> block `manyTill` string e + case c of + '[' -> return $ OptArg b + '{' -> return $ FixArg b + _ -> error "this cannot happen!" -p_Sym :: Parser TeXArg -p_Sym = p_SimpleArg ('<','>') SymArg + whitespace :: Parser () + whitespace = try (do _ <- char ' ' + whitespace) + <|> return () -p_MSym :: Parser TeXArg -p_MSym = p_MultiArg ('<','>') MSymArg + ------------------------------------------------------------------------ + -- Special commands (consisting of one char) + ------------------------------------------------------------------------ + special :: Parser LaTeX + special = do + x <- anyChar + case x of + '(' -> math Parentheses endPa + '[' -> math Square endSq + '{' -> lbrace + '}' -> rbrace + '|' -> vert + '\\' -> lbreak + _ -> commS [x] -p_Arg :: Parser TeXArg -p_Arg = choice $ try <$> [ p_MOpt , p_MSym , p_Opt , p_Fix , p_Sym ] + ------------------------------------------------------------------------ + -- line break + ------------------------------------------------------------------------ + lbreak :: Parser LaTeX + lbreak = do + y <- try (char oSq <|> char str <|> return ' ') + case y of + '[' -> linebreak False + '*' -> do z <- try (char oSq <|> return ' ') + case z of + '[' -> linebreak True + _ -> return (TeXLineBreak Nothing True) + _ -> return (TeXLineBreak Nothing False) --- | Parse a LaTeX expression written in 'Text', to a 'LaTeX' AST. --- It returns a 'ParseError' in case of parsing error. -parseLaTeX :: Text -> Either ParseError LaTeX -parseLaTeX = parse (p_TeX >>= \l -> eof >> return l) "LaTeX input"+ linebreak :: Bool -> Parser LaTeX + linebreak t = do m <- measure + _ <- char eSq + s <- try (char str <|> return ' ') + return $ TeXLineBreak (Just m) (t || s == str) + + measure :: Parser Measure + measure = try (double >>= unit) + <|> CustomMeasure <$> block + + unit :: Double -> Parser Measure + unit f = do + u1 <- anyChar + u2 <- anyChar + case map toLower [u1, u2] of + "pt" -> return $ Pt (truncate f) + "mm" -> return $ Mm f + "cm" -> return $ Cm f + "in" -> return $ In f + "ex" -> return $ Ex f + "em" -> return $ Em f + _ -> fail "NaN" + + ------------------------------------------------------------------------ + -- right or left brace or vertical + ------------------------------------------------------------------------ + rbrace, lbrace,vert :: Parser LaTeX + lbrace = brace "{" + rbrace = brace "}" + vert = brace "|" + + brace :: String -> Parser LaTeX + brace = return . TeXCommS + + commS :: String -> Parser LaTeX + commS = return . TeXCommS + + ------------------------------------------------------------------------ + -- Math + ------------------------------------------------------------------------ + dolMath :: Parser LaTeX + dolMath = do + _ <- char dol + b <- mconcat <$> block `manyTill` char dol + return $ TeXMath Dollar b -- [] + + math :: MathType -> Text -> Parser LaTeX + math t eMath = do + b <- mconcat <$> block `manyTill` try (string eMath) + return $ TeXMath t b -- [] + + ------------------------------------------------------------------------ + -- Comment + ------------------------------------------------------------------------ + comment :: Parser LaTeX + comment = do + _ <- char per + c <- A.takeTill (== '\n') + e <- atEnd + unless e (char '\n' >>= \_ -> return ()) + return $ TeXComment c + + ------------------------------------------------------------------------ + -- Helpers + ------------------------------------------------------------------------ + isSpecial :: Char -> Bool + isSpecial = (`elem` specials) -- [bsl, oSq, oPa, oBr, eBr] + + begin, end :: Text + begin = T.pack "\\begin" + end = T.pack "\\end" + + endCmd :: Char -> Bool + endCmd = flip elem symbols + + nul, eol, spc :: Char + nul = '\0' + eol = '\n' + spc = ' ' + + oBr, eBr, oSq, eSq, oPa, ePa, bsl, dol, per, str :: Char + oBr = '{' + eBr = '}' + oSq = '[' + eSq = ']' + oPa = '(' + ePa = ')' + bsl = '\\' + dol = '$' + per = '%' + str = '*' + + endPa, endSq, endlessBr, endlessSq :: Text + endPa = T.pack "\\)" + endSq = T.pack "\\]" + endlessBr = T.pack "}" + endlessSq = T.pack "]" + + emptyArg :: Text + emptyArg = T.pack "{}" + + symbols :: String + symbols = [nul, eol, spc, oBr, eBr, eSq, oSq, oPa, ePa, bsl, dol, per] + + specials :: String + specials = "'(),.-\"!^$&#{}%~|/:;=[]\\` "
Text/LaTeX/Base/Render.hs view
@@ -34,11 +34,24 @@ -- You mainly will use this to obtain the 'Text' output -- of a 'LaTeX' value. If you are going to write the result -- in a file, consider to use 'renderFile'. +-- +-- Consider also to use 'rendertex' to get 'Render'able values +-- into 'LaTeX' blocks. +-- +-- If you want to make a type instance of 'Render' and you already +-- have a 'Show' instance, you can use the default instance. +-- +-- > render = fromString . show +-- class Show a => Render a where render :: a -> Text -- render = fromString . show +-- | This instance escapes LaTeX reserved characters. +instance Render Text where + render = protectText + -- | Render every element of a list and append results. renderAppend :: Render a => [a] -> Text renderAppend = mconcat . fmap render @@ -74,34 +87,59 @@ rendertex :: (Render a,LaTeXC l) => a -> l rendertex = fromLaTeX . TeXRaw . render +-- Render instances + +instance Render Measure where + render (Pt x) = render x <> "pt" + render (Mm x) = render x <> "mm" + render (Cm x) = render x <> "cm" + render (In x) = render x <> "in" + render (Ex x) = render x <> "ex" + render (Em x) = render x <> "em" + render (CustomMeasure x) = render x + -- LaTeX instances instance Render LaTeX where - render (TeXRaw t) = t - render (TeXComm name []) = "\\" <> fromString name <> "{}" - render (TeXComm name args) = - "\\" - <> fromString name - <> renderAppend args - render (TeXCommS name) = "\\" <> fromString name - render (TeXEnv name args c) = - "\\begin{" - <> fromString name - <> "}" - <> renderAppend args - <> render c - <> "\\end{" - <> fromString name - <> "}" - render (TeXMath l) = "$" <> render l <> "$" - render (TeXNewLine b) = " \\\\" <> ( if b then "*" else mempty ) <> " " - render (TeXOp sym l1 l2) = render l1 <> fromString sym <> render l2 - render (TeXBraces l) = "{" <> render l <> "}" - render (TeXComment c) = - let xs = Data.Text.lines c - in (" " <>) $ Data.Text.unlines $ fmap ("% " <>) xs - render (TeXSeq l1 l2) = render l1 <> render l2 - render TeXEmpty = mempty + + render (TeXRaw t) = t + + render (TeXComm name []) = "\\" <> fromString name <> "{}" + render (TeXComm name args) = + "\\" + <> fromString name + <> renderAppend args + render (TeXCommS name) = "\\" <> fromString name + + render (TeXEnv name args c) = + "\\begin{" + <> fromString name + <> "}" + <> renderAppend args + <> render c + <> "\\end{" + <> fromString name + <> "}" + + render (TeXMath Dollar l) = "$" <> render l <> "$" + render (TeXMath Square l) = "\\[" <> render l <> "\\]" + render (TeXMath Parentheses l) = "\\(" <> render l <> "\\)" + + render (TeXLineBreak m b) = "\\\\" <> maybe mempty (\x -> "[" <> render x <> "]") m <> ( if b then "*" else mempty ) + + render (TeXOp sym l1 l2) = render l1 <> fromString sym <> render l2 + + render (TeXBraces l) = "{" <> render l <> "}" + + render (TeXComment c) = + let xs = Data.Text.lines c + in if null xs then "%\n" + else Data.Text.unlines $ fmap ("%" <>) xs + + render (TeXSeq l1 l2) = render l1 <> render l2 + render TeXEmpty = mempty + + instance Render TeXArg where render (OptArg l) = "[" <> render l <> "]"
Text/LaTeX/Base/Syntax.hs view
@@ -6,7 +6,9 @@ -- the library, import this module and use 'LaTeX' data constructors. module Text.LaTeX.Base.Syntax ( -- * @LaTeX@ datatype - LaTeX (..) + Measure (..) + , MathType (..) + , LaTeX (..) , TeXArg (..) , (<>) -- * Escaping reserved characters @@ -18,6 +20,27 @@ import Data.Monoid import Data.String +-- | Measure units defined in LaTeX. Use 'CustomMeasure' to use commands like 'textwidth'. +-- For instance: +-- +-- > rule Nothing (CustomMeasure linewidth) (Pt 2) +-- +-- This will create a black box (see 'rule') as wide as the text and two points tall. +-- +data Measure = + Pt Int -- ^ A point is 1/72.27 inch, that means about 0.0138 inch or 0.3515 mm. + | Mm Double -- ^ Millimeter. + | Cm Double -- ^ Centimeter. + | In Double -- ^ Inch. + | Ex Double -- ^ The height of an \"x\" in the current font. + | Em Double -- ^ The width of an \"M\" in the current font. + | CustomMeasure LaTeX -- ^ You can introduce a 'LaTeX' expression as a measure. + deriving (Eq, Show) + +-- | Different types of syntax for mathematical expressions. +data MathType = Parentheses | Square | Dollar + deriving (Eq,Show,Ord) + -- | Type of @LaTeX@ blocks. data LaTeX = TeXRaw Text -- ^ Raw text. @@ -29,8 +52,8 @@ -- First argument is the name of the environment. -- Second, its arguments. -- Third, its content. - | TeXMath LaTeX -- ^ Mathematical expressions. - | TeXNewLine Bool -- ^ Newline character. + | TeXMath MathType LaTeX -- ^ Mathematical expressions. + | TeXLineBreak (Maybe Measure) Bool -- ^ Line break command. | TeXOp String LaTeX LaTeX -- ^ Operators. | TeXBraces LaTeX -- ^ A expression between braces. | TeXComment Text -- ^ Comments.
Text/LaTeX/Base/Types.hs view
@@ -7,7 +7,7 @@ , PackageName , Label , createLabel , labelName - , Pos (..) + , Pos (..) , HPos (..) , TableSpec (..) , Measure (..) ) where @@ -23,9 +23,11 @@ newtype Label = Label String deriving (Eq, Show) +-- | Create a label from its name. createLabel :: String -> Label createLabel = Label +-- | Get the name of a label. labelName :: Label -> String labelName (Label str) = str @@ -35,7 +37,7 @@ instance IsString Label where fromString = createLabel --- | A vertical position. +-- | Vertical position. data Pos = Bottom | Center | Top deriving Show instance Render Pos where @@ -43,6 +45,14 @@ render Center = "c" render Top = "t" +-- | Horizontal position. +data HPos = HLeft | HCenter | HRight deriving Show + +instance Render HPos where + render HLeft = "l" + render HCenter = "c" + render HRight = "r" + data TableSpec = LeftColumn -- ^ Left-justified column. | CenterColumn -- ^ Centered column. @@ -62,23 +72,4 @@ render (ParColumnMid l) = "m" <> render (FixArg l) render (ParColumnBot l) = "b" <> render (FixArg l) render VerticalLine = "|" - render DVerticalLine = "||" - -data Measure = - Pt Int -- ^ A point is 1/72.27 inch, that means about 0.0138 inch or 0.3515 mm. - | Mm Float -- ^ Millimeter. - | Cm Float -- ^ Centimeter. - | In Float -- ^ Inch. - | Ex Float -- ^ The height of an \"x\" in the current font. - | Em Float -- ^ The width of an \"M\" in the current font. - | CustomMeasure LaTeX -- ^ You can introduce a 'LaTeX' expression as a measure. - deriving Show - -instance Render Measure where - render (Pt x) = render x <> "pt" - render (Mm x) = render x <> "mm" - render (Cm x) = render x <> "cm" - render (In x) = render x <> "in" - render (Ex x) = render x <> "ex" - render (Em x) = render x <> "em" - render (CustomMeasure x) = render x+ render DVerticalLine = "||"
Text/LaTeX/Base/Warnings.hs view
@@ -108,7 +108,7 @@ "ref" -> newref n "pageref" -> newref n labcheck (TeXEnv _ _ l) = labcheck l -labcheck (TeXMath l) = labcheck l +labcheck (TeXMath _ l) = labcheck l labcheck (TeXOp _ l1 l2) = labcheck l1 >> labcheck l2 labcheck (TeXBraces l) = labcheck l labcheck (TeXSeq l1 l2) = labcheck l1 >> labcheck l2
Text/LaTeX/Base/Writer.hs view
@@ -1,6 +1,4 @@ -{-# LANGUAGE GeneralizedNewtypeDeriving #-} - -- | The writer monad applied to 'LaTeX' values. Useful to compose 'LaTeX' values -- using the @do@ notation: -- @@ -19,7 +17,7 @@ -- -- Since 'LaTeXT' is a monad transformer, you can do also: -- --- > anotherExample :: Monad m => LaTeXT m () +-- > anotherExample :: LaTeXT IO () -- > anotherExample = lift (readFile "foo") >>= verbatim . fromString -- -- This way, it is easy (without carrying arguments) to include IO outputs @@ -69,23 +67,28 @@ newtype LaTeXT m a = LaTeXT { unwrapLaTeXT :: WriterT LaTeX m (a,Maybe String) } -instance Functor m => Functor (LaTeXT m) where +instance Functor f => Functor (LaTeXT f) where fmap f (LaTeXT c) = LaTeXT $ fmap (first f) c -type LaTeXT_ m = LaTeXT m () - pairNoth :: a -> (a,Maybe b) pairNoth x = (x,Nothing) +instance Applicative f => Applicative (LaTeXT f) where + pure = LaTeXT . pure . pairNoth + (LaTeXT f) <*> (LaTeXT x) = LaTeXT $ fmap (first . fst) f <*> x + +type LaTeXT_ m = LaTeXT m () + instance MonadTrans LaTeXT where lift = LaTeXT . liftM pairNoth . lift instance Monad m => Monad (LaTeXT m) where - return = lift . return + return = LaTeXT . return . pairNoth (LaTeXT c) >>= f = LaTeXT $ do (a,_) <- c let LaTeXT c' = f a c' + fail = throwError instance MonadIO m => MonadIO (LaTeXT m) where liftIO = lift . liftIO @@ -134,8 +137,8 @@ (p,l) <- lift $ runWriterT c tell $ f l return p - + -- | Lift an operator over 'LaTeX' values to an operator -- acting over the state of two 'LaTeXT' computations. -- @@ -176,5 +179,5 @@ -- | 'mappend' @=@ '>>'. instance Monad m => Monoid (LaTeXT m a) where - mempty = return undefined + mempty = throwError "LaTeXT: mempty!" mappend = (>>)
Text/LaTeX/Packages/AMSMath.hs view
@@ -1,11 +1,24 @@ {-# LANGUAGE OverloadedStrings #-} +-- | \AMSMath\ support. Also numeric instances ('Num', 'Fractional' and 'Floating') for 'LaTeX' and 'LaTeXT'. module Text.LaTeX.Packages.AMSMath ( -- * AMSMath package amsmath -- * AMSMath functions - , math + , math, mathDisplay -- * Symbols and utilities + -- | The unicode approximations do, of course, not reliably represent how + -- LaTeX renders these symbols. + + -- ** Brackets / delimiters + , autoParens + , autoSquareBrackets, autoBraces, autoAngleBrackets + , autoBrackets + + , langle , rangle + , lfloor , rfloor + , lceil , rceil + , dblPipe -- ** Superscript and subscript , (^:) , (!:) -- ** Function symbols @@ -14,13 +27,36 @@ , tsin , arcsin , tcos , arccos , ttan , arctan + , cot , arccot + , tsinh , tcosh , ttanh , coth + , sec , csc , texp , tlog , ln + , tsqrt -- ** Operator symbols + -- *** Arithmetic + , pm , mp + , cdot , times , div_ + , frac + , (*:) , star + , circ , bullet + -- *** Comparison , (=:) , (/=:) - , (>:) , (>=:) , (<:) , (<=:) + , (>:) , (>=:) + , ll , gg + , equiv + , propto + -- *** Sets , in_ , ni , notin + , subset , supset + , cap , cup + , setminus + -- *** Misc operators + , vee , wedge + , oplus , ominus , otimes + , oslash , odot + -- ** Greek alphabet -- | Functions of greek alphabet symbols. -- @@ -31,7 +67,7 @@ , alpha , beta , gamma , gammau , delta , deltau , epsilon , varepsilon , zeta - , eta , theta , thetau + , eta , theta , vartheta , thetau , iota , kappa , lambda , lambdau , mu , nu , xi , xiu , pi_ @@ -42,22 +78,28 @@ , phiu , chi , psi , psiu , omega , omegau -- ** Other symbols - , to - , forall + , to , mapsto + , forall , exists , dagger, ddagger -- * Fonts + , mathdefault , mathbf , mathrm , mathcal , mathsf , mathtt , mathit + -- * Matrices + , pmatrix , bmatrix + , b2matrix , vmatrix + , v2matrix ) where +import Text.LaTeX.Base import Text.LaTeX.Base.Syntax import Text.LaTeX.Base.Class -import Text.LaTeX.Base.Commands (raw,between) -import Text.LaTeX.Base.Types +-- Matrices +import Data.Matrix -- | AMSMath package. -- Example: @@ -68,11 +110,159 @@ -- | Inline mathematical expressions. math :: LaTeXC l => l -> l -math = liftL TeXMath +math = liftL $ TeXMath Dollar +-- | Displayed mathematical expressions, i.e. in a seperate line / block. +mathDisplay :: LaTeXC l => l -> l +mathDisplay = liftL $ TeXMath Square + +------------------------------------------------------- +-- Numeric instances for LaTeX and LaTeXT -- +------------------------------------------------------- + +----------- LaTeX instances + +-- | Careful! Method 'signum' is undefined. Don't use it! +instance Num LaTeX where + (+) = TeXOp "+" + (-) = TeXOp "-" + (*) = (<>) + negate = (TeXEmpty -) + fromInteger = rendertex + abs = autoBrackets "|" "|" + -- Non-defined methods + signum _ = error "Cannot use \"signum\" Num method with a LaTeX value." + +-- | Division uses the LaTeX 'frac' command. +instance Fractional LaTeX where + (/) = frac + fromRational = rendertex . (fromRational :: Rational -> Double) + +-- | Undefined methods: 'asinh', 'atanh' and 'acosh'. +instance Floating LaTeX where + pi = pi_ + exp = (texp <>) + sqrt = tsqrt Nothing + log = (tlog <>) + (**) = (^:) + logBase b x = (tlog !: b) <> x + sin = (tsin <>) + tan = (ttan <>) + cos = (tcos <>) + asin = (arcsin <>) + atan = (arctan <>) + acos = (arccos <>) + sinh = (tsinh <>) + tanh = (ttanh <>) + cosh = (tcosh <>) + -- Non-defined methods + asinh = error "Function 'asinh' is not defined in AMSMath!" + atanh = error "Function 'atabh' is not defined in AMSMath!" + acosh = error "Function 'acosh' is not defined in AMSMath!" + +----------- LaTeXT instances + +-- | Warning: this instance only exists for the 'Num' instance. +instance Monad m => Eq (LaTeXT m a) where + _ == _ = error "Cannot use \"(==)\" Eq method with a LaTeXT value." + +-- | Warning: this instance only exists for the 'Num' instance. +instance Monad m => Show (LaTeXT m a) where + show _ = error "Cannot use \"show\" Show method with a LaTeXT value." + +-- | Careful! Method 'signum' is undefined. Don't use it! +instance Monad m => Num (LaTeXT m a) where + (+) = liftOp (+) + (-) = liftOp (-) + (*) = (>>) + negate = liftFun negate + fromInteger = fromLaTeX . fromInteger + abs = liftFun abs + -- Non-defined methods + signum _ = error "Cannot use \"signum\" Num method with a LaTeXT value." + +-- | Division uses the LaTeX 'frac' command. +instance Monad m => Fractional (LaTeXT m a) where + (/) = liftOp (/) + fromRational = fromLaTeX . fromRational + +-- | Undefined methods: 'asinh', 'atanh' and 'acosh'. +instance Monad m => Floating (LaTeXT m a) where + pi = pi_ + exp = liftFun exp + sqrt = liftFun sqrt + log = liftFun log + (**) = liftOp (**) + logBase = liftOp logBase + sin = liftFun sin + tan = liftFun tan + cos = liftFun cos + asin = liftFun asin + atan = liftFun atan + acos = liftFun acos + sinh = liftFun sinh + tanh = liftFun tanh + cosh = liftFun cosh + -- Non-defined methods + asinh = error "Function 'asinh' is not defined in AMSMath!" + atanh = error "Function 'atabh' is not defined in AMSMath!" + acosh = error "Function 'acosh' is not defined in AMSMath!" + ------------------------------------- ------- Symbols and utilities ------- +-- | Surround a LaTeX math expression by parentheses whose height +-- automatically matches the expression's. Translates to @\\left(...\\right)@. +autoParens :: LaTeXC l => l -> l +autoParens x = comm0 "left(" <> x <> comm0 "right)" + +-- | Like 'autoParens', but with square brackets. Equivalent to @'autoBrackets'\"[\"\"]\"@. +autoSquareBrackets :: LaTeXC l => l -> l +autoSquareBrackets x = comm0 "left[" <> x <> comm0 "right]" + +-- | Like 'autoParens', but with curly brackets. +autoBraces :: LaTeXC l => l -> l +autoBraces x = comm0 "left"<>"{" <> x <> comm0 "right"<>"}" + +-- | Like 'autoParens', but with angle brackets 〈 ... 〉. Equivalent to @'autoBrackets' 'langle' 'rangle'@. +autoAngleBrackets :: LaTeXC l => l -> l +autoAngleBrackets x = comm0 "left"<>langle <> x <> comm0 "right"<>rangle + +-- | Use custom LaTeX expressions as auto-scaled delimiters to surround math. +-- Suitable delimiters include |...| (absolute value), ‖...‖ (norm, +-- 'dblPipe'), ⌊...⌋ (round-off Gauss brackets, 'lfloor' / 'rfloor') etc.. +autoBrackets :: LaTeXC l => LaTeX -> LaTeX -> l -> l +autoBrackets lBrack rBrack x + = comm0 "left" <> braces (fromLaTeX lBrack) <> x <> comm0 "right" <> braces (fromLaTeX rBrack) + +-- | Left angle bracket, 〈. +langle :: LaTeXC l => l +langle = comm0 "langle" + +-- | Right angle bracket, 〉. +rangle :: LaTeXC l => l +rangle = comm0 "rangle" + +-- | Left floor, ⌊. +lfloor :: LaTeXC l => l +lfloor = comm0 "lfloor" + +-- | Right floor, ⌋. +rfloor :: LaTeXC l => l +rfloor = comm0 "rfloor" + +-- | Left ceiling, ⌈. +lceil :: LaTeXC l => l +lceil = comm0 "lceil" + +-- | Right ceiling, ⌉. +rceil :: LaTeXC l => l +rceil = comm0 "rceil" + +-- | Double vertical line, used as delimiter for norms (‖ ... ‖). +dblPipe :: LaTeXC l => l +dblPipe = comm0 "|" + -- | Superscript. (^:) :: LaTeXC l => l -> l -> l x ^: y = x <> raw "^" <> braces y @@ -107,6 +297,38 @@ arctan :: LaTeXC l => l arctan = comm0 "arctan" +-- | Cotangent function symbol. +cot :: LaTeXC l => l +cot = comm0 "cot" + +-- | Arccotangent function symbol. +arccot :: LaTeXC l => l +arccot = comm0 "arccot" + +-- | Hyperbolic sine function symbol. +tsinh :: LaTeXC l => l +tsinh = comm0 "sinh" + +-- | Hyperbolic cosine function symbol. +tcosh :: LaTeXC l => l +tcosh = comm0 "cosh" + +-- | Hyperbolic tangent function symbol. +ttanh :: LaTeXC l => l +ttanh = comm0 "tanh" + +-- | Hyperbolic cotangent function symbol. +coth :: LaTeXC l => l +coth = comm0 "coth" + +-- | Secant function symbol. +sec :: LaTeXC l => l +sec = comm0 "sec" + +-- | Cosecant function symbol. +csc :: LaTeXC l => l +csc = comm0 "csc" + -- | Exponential function symbol. texp :: LaTeXC l => l texp = comm0 "exp" @@ -119,16 +341,67 @@ ln :: LaTeXC l => l ln = comm0 "ln" +-- | Root notation. Use @tsqrt (Just n) x@ for the @n@th root of @x@. +-- When 'Nothing' is supplied, the function will output a square root. +tsqrt :: LaTeXC l => Maybe l -> l -> l +tsqrt Nothing = liftL $ \x -> TeXComm "sqrt" [FixArg x] +tsqrt (Just n) = liftL2 (\n x -> TeXComm "sqrt" [OptArg n, FixArg x]) n + ---- Operator symbols -- | Negative form of an operator. notop :: LaTeXC l => (l -> l -> l) - -> (l -> l -> l) + -> l -> l -> l notop op = \l1 l2 -> (l1 <> commS "not") `op` l2 + +-- | Plus-or-minus operator (±). +pm :: LaTeXC l => l -> l -> l +pm = between $ comm0 "pm" + +-- | Minus-or-plus operator (∓). +mp :: LaTeXC l => l -> l -> l +mp = between $ comm0 "mp" + +-- | Centered-dot operator (⋅). +cdot :: LaTeXC l => l -> l -> l +cdot = between $ comm0 "cdot" + +-- | \"x-cross\" multiplication operator (×). +times :: LaTeXC l => l -> l -> l +times = between $ comm0 "times" + +-- | Division operator (. +div_ :: LaTeXC l => l -> l -> l +div_ = between $ comm0 "div" + +-- | Fraction operator. +frac :: LaTeXC l => l -> l -> l +frac = liftL2 $ \p q -> TeXComm "frac" [FixArg p, FixArg q] + +infixl 7 *: + +-- | Asterisk operator (*). +-- +-- > infixl 7 *: +(*:) :: LaTeXC l => l -> l -> l +(*:) = between $ comm0 "ast" + +-- | Star operator (★). +star :: LaTeXC l => l -> l -> l +star = between $ comm0 "star" + +-- | Ring operator (∘). +circ :: LaTeXC l => l -> l -> l +circ = between $ comm0 "circ" + +-- | Bullet operator (∙). +bullet :: LaTeXC l => l -> l -> l +bullet = between $ comm0 "bullet" + infixr 4 =: , /=: -- | Equal. @@ -137,7 +410,7 @@ (=:) :: LaTeXC l => l -> l -> l (=:) = liftL2 $ TeXOp "=" --- | Not equal. +-- | Not equal (≠). -- -- > infixr 4 /=: (/=:) :: LaTeXC l => l -> l -> l @@ -147,7 +420,7 @@ (>:) :: LaTeXC l => l -> l -> l (>:) = liftL2 $ TeXOp ">" --- | Greater or equal. +-- | Greater or equal (≥). (>=:) :: LaTeXC l => l -> l -> l (>=:) = between $ comm0 "geq" @@ -155,173 +428,364 @@ (<:) :: LaTeXC l => l -> l -> l (<:) = liftL2 $ TeXOp "<" --- | Lesser or equal. +-- | Lesser or equal (≤). (<=:) :: LaTeXC l => l -> l -> l (<=:) = between $ comm0 "leq" +-- | Much less (≪). +ll :: LaTeXC l => l -> l -> l +ll = between $ comm0 "ll" + +-- | Much greater (≫). +gg :: LaTeXC l => l -> l -> l +gg = between $ comm0 "gg" + +-- | Proportional-to (∝). +propto :: LaTeXC l => l -> l -> l +propto = between $ comm0 "propto" + +-- | Identical \/ defined-as \/ equivalent (≡). +equiv :: LaTeXC l => l -> l -> l +equiv = between $ comm0 "equiv" + + +-- | Element-of (∈). in_ :: LaTeXC l => l -> l -> l in_ = between $ comm0 "in" +-- | Mirrored element-of (∋). ni :: LaTeXC l => l -> l -> l ni = between $ comm0 "ni" +-- | Not element of (∉). notin :: LaTeXC l => l -> l -> l notin = between $ comm0 "notin" +-- | Subset-of (⊂). +subset :: LaTeXC l => l -> l -> l +subset = between $ comm0 "subset" + +-- | Superset-of (⊃). +supset :: LaTeXC l => l -> l -> l +supset = between $ comm0 "supset" + +-- | Set intersection (∩). +cap :: LaTeXC l => l -> l -> l +cap = between $ comm0 "cap" + +-- | Set union (∪). +cup :: LaTeXC l => l -> l -> l +cup = between $ comm0 "cup" + +-- | Set minus (∖). +setminus :: LaTeXC l => l -> l -> l +setminus = between $ comm0 "setminus" + + +-- | Angle pointing downwards (∨). +vee :: LaTeXC l => l -> l -> l +vee = between $ comm0 "vee" + +-- | Angle pointing upwards (∧). +wedge :: LaTeXC l => l -> l -> l +wedge = between $ comm0 "wedge" + + +-- | Circled plus operator (⊕). +oplus :: LaTeXC l => l -> l -> l +oplus = between $ comm0 "oplus" + +-- | Circled minus operator (⊖). +ominus :: LaTeXC l => l -> l -> l +ominus = between $ comm0 "ominus" + +-- | Circled multiplication cross (⊗). +otimes :: LaTeXC l => l -> l -> l +otimes = between $ comm0 "otimes" + +-- | Circled slash (⊘). +oslash :: LaTeXC l => l -> l -> l +oslash = between $ comm0 "oslash" + +-- | Circled dot operator (⊙). +odot :: LaTeXC l => l -> l -> l +odot = between $ comm0 "odot" + + + + ---- Greek alphabet +-- | /α/ symbol. alpha :: LaTeXC l => l alpha = comm0 "alpha" +-- | /β/ symbol. beta :: LaTeXC l => l beta = comm0 "beta" +-- | /γ/ symbol. gamma :: LaTeXC l => l gamma = comm0 "gamma" +-- | Γ symbol. gammau :: LaTeXC l => l gammau = comm0 "Gamma" +-- | /δ/ symbol. delta :: LaTeXC l => l delta = comm0 "delta" +-- | Δ symbol. deltau :: LaTeXC l => l deltau = comm0 "Delta" +-- | /ϵ/ symbol. epsilon :: LaTeXC l => l epsilon = comm0 "epsilon" +-- | /ε/ symbol. varepsilon :: LaTeXC l => l varepsilon = comm0 "varepsilon" +-- | /ζ/ symbol. zeta :: LaTeXC l => l zeta = comm0 "zeta" +-- | /η/ symbol. eta :: LaTeXC l => l eta = comm0 "eta" +-- | /θ/ symbol. theta :: LaTeXC l => l theta = comm0 "theta" +-- | /ϑ/ symbol. +vartheta :: LaTeXC l => l +vartheta = comm0 "vartheta" + +-- | Θ symbol. thetau :: LaTeXC l => l thetau = comm0 "thetau" +-- | /ι/ symbol. iota :: LaTeXC l => l iota = comm0 "iota" +-- | /κ/ symbol. kappa :: LaTeXC l => l kappa = comm0 "kappa" +-- | /λ/ symbol. lambda :: LaTeXC l => l lambda = comm0 "lambda" +-- | Λ symbol. lambdau :: LaTeXC l => l lambdau = comm0 "Lambda" +-- | /μ/ symbol. mu :: LaTeXC l => l mu = comm0 "mu" +-- | /ν/ symbol. nu :: LaTeXC l => l nu = comm0 "nu" +-- | /ξ/ symbol. xi :: LaTeXC l => l xi = comm0 "xi" +-- | Ξ symbol. xiu :: LaTeXC l => l xiu = comm0 "Xi" +-- | /π/ symbol. pi_ :: LaTeXC l => l pi_ = comm0 "pi" +-- | /ϖ/ symbol. varpi :: LaTeXC l => l varpi = comm0 "varpi" +-- | Π symbol. piu :: LaTeXC l => l piu = comm0 "Pi" +-- | /ρ/ symbol. rho :: LaTeXC l => l rho = comm0 "rho" +-- | /ϱ/ symbol. varrho :: LaTeXC l => l varrho = comm0 "varrho" +-- | /σ/ symbol. sigma :: LaTeXC l => l sigma = comm0 "sigma" +-- | /ς/ symbol. varsigma :: LaTeXC l => l varsigma = comm0 "varsigma" +-- | Σ symbol. sigmau :: LaTeXC l => l sigmau = comm0 "Sigma" +-- | /τ/ symbol. tau :: LaTeXC l => l tau = comm0 "tau" +-- | /υ/ symbol. upsilon :: LaTeXC l => l upsilon = comm0 "upsilon" +-- | Υ symbol. upsilonu :: LaTeXC l => l upsilonu = comm0 "Upsilon" +-- | /ϕ/ symbol. phi :: LaTeXC l => l phi = comm0 "phi" +-- | /φ/ symbol. varphi :: LaTeXC l => l varphi = comm0 "varphi" +-- | Φ symbol. phiu :: LaTeXC l => l phiu = comm0 "Phi" +-- | /χ/ symbol. chi :: LaTeXC l => l chi = comm0 "chi" +-- | /ψ/ symbol. psi :: LaTeXC l => l psi = comm0 "psi" +-- | Ψ symbol. psiu :: LaTeXC l => l psiu = comm0 "Psi" +-- | /ω/ symbol. omega :: LaTeXC l => l omega = comm0 "omega" +-- | Ω symbol. omegau :: LaTeXC l => l omegau = comm0 "Omega" ---- Other symbols --- | A right-arrow. +-- | A right-arrow, →. to :: LaTeXC l => l to = comm0 "to" --- | /For all/ symbol. +-- | A right-arrow for function definitions, ↦. +mapsto :: LaTeXC l => l +mapsto = comm0 "mapsto" + +-- | /For all/ symbol, ∀. forall :: LaTeXC l => l forall = comm0 "forall" --- | Dagger symbol. +-- | /Exists/ symbol, ∃. +exists :: LaTeXC l => l +exists = comm0 "exists" + +-- | Dagger symbol, †. dagger :: LaTeXC l => l dagger = comm0 "dagger" --- | Double dagger symbol. +-- | Double dagger symbol, ‡. ddagger :: LaTeXC l => l ddagger = comm0 "ddagger" ------------------------------------- ------------ Math Fonts ------------- +-- | Default math symbol font. +mathdefault :: LaTeXC l => l -> l +mathdefault = liftL $ \l -> TeXComm "mathdefault" [FixArg l] + +-- | Bold face. mathbf :: LaTeXC l => l -> l mathbf = liftL $ \l -> TeXComm "mathbf" [FixArg l] +-- | Roman, i.e. not-italic math. mathrm :: LaTeXC l => l -> l -mathrm =liftL $ \l -> TeXComm "mathrm" [FixArg l] +mathrm = liftL $ \l -> TeXComm "mathrm" [FixArg l] +-- | Calligraphic math symbols. mathcal :: LaTeXC l => l -> l mathcal = liftL $ \l -> TeXComm "mathcal" [FixArg l] +-- | Sans-serif math. mathsf :: LaTeXC l => l -> l mathsf = liftL $ \l -> TeXComm "mathsf" [FixArg l] +-- | Typewriter font. mathtt :: LaTeXC l => l -> l mathtt = liftL $ \l -> TeXComm "mathtt" [FixArg l] +-- | Italic math. Uses the same glyphs as 'mathdefault', but with spacings +-- intended for multi-character symbols rather than juxtaposition of single-character symbols. mathit :: LaTeXC l => l -> l mathit = liftL $ \l -> TeXComm "mathit" [FixArg l] + +------------------------------------- +------------- Matrices -------------- + +matrix2tex :: Render a => Matrix a -> LaTeX +matrix2tex m = mconcat + [ foldr1 (&) [ rendertex $ m ! (i,j) + | j <- [1 .. ncols m] + ] <> lnbk + | i <- [1 .. nrows m] + ] + +-- | LaTeX rendering of a matrix using @pmatrix@.Optional argument sets the alignment +-- of the cells. Default (providing 'Nothing') is centered. +-- +-- > ( M ) +-- +pmatrix :: (Render a, LaTeXC l) => Maybe HPos -> Matrix a -> l +pmatrix Nothing = fromLaTeX . TeXEnv "pmatrix" [] . matrix2tex +pmatrix (Just p) = fromLaTeX . TeXEnv "pmatrix*" [OptArg $ rendertex p] . matrix2tex + +-- | LaTeX rendering of a matrix using @bmatrix@. Optional argument sets the alignment +-- of the cells. Default (providing 'Nothing') is centered. +-- +-- > [ M ] +-- +bmatrix :: (Render a, LaTeXC l) => Maybe HPos -> Matrix a -> l +bmatrix Nothing = fromLaTeX . TeXEnv "bmatrix" [] . matrix2tex +bmatrix (Just p) = fromLaTeX . TeXEnv "bmatrix*" [OptArg $ rendertex p] . matrix2tex + +-- | LaTeX rendering of a matrix using @Bmatrix@. Optional argument sets the alignment +-- of the cells. Default (providing 'Nothing') is centered. +-- +-- > { M } +-- +b2matrix :: (Render a, LaTeXC l) => Maybe HPos -> Matrix a -> l +b2matrix Nothing = fromLaTeX . TeXEnv "Bmatrix" [] . matrix2tex +b2matrix (Just p) = fromLaTeX . TeXEnv "Bmatrix*" [OptArg $ rendertex p] . matrix2tex + +-- | LaTeX rendering of a matrix using @vmatrix@. Optional argument sets the alignment +-- of the cells. Default (providing 'Nothing') is centered. +-- +-- > | M | +-- +vmatrix :: (Render a, LaTeXC l) => Maybe HPos -> Matrix a -> l +vmatrix Nothing = fromLaTeX . TeXEnv "vmatrix" [] . matrix2tex +vmatrix (Just p) = fromLaTeX . TeXEnv "vmatrix*" [OptArg $ rendertex p] . matrix2tex + +-- | LaTeX rendering of a matrix using @Vmatrix@. Optional argument sets the alignment +-- of the cells. Default (providing 'Nothing') is centered. +-- +-- > || M || +-- +v2matrix :: (Render a, LaTeXC l) => Maybe HPos -> Matrix a -> l +v2matrix Nothing = fromLaTeX . TeXEnv "Vmatrix" [] . matrix2tex +v2matrix (Just p) = fromLaTeX . TeXEnv "Vmatrix*" [OptArg $ rendertex p] . matrix2tex
Text/LaTeX/Packages/Beamer.hs view
@@ -1,5 +1,6 @@ {-# LANGUAGE OverloadedStrings #-} +-- | Beamer is a LaTeX package for the creation of slides. module Text.LaTeX.Packages.Beamer ( -- * Beamer package beamer @@ -44,7 +45,7 @@ framesubtitle :: LaTeXC l => l -> l framesubtitle = liftL $ \l -> TeXComm "framesubtitle" [FixArg l] --- | Highlight in red a piece text. With the 'OverlaySpec's, you can specify the slides where +-- | Highlight in red a piece of text. With the 'OverlaySpec's, you can specify the slides where -- the text will be highlighted. alert :: LaTeXC l => [OverlaySpec] -> l -> l alert os = liftL $ \l -> TeXComm "alert" [ MSymArg $ fmap (TeXRaw . render) os, FixArg l] @@ -62,10 +63,13 @@ uncover :: LaTeXC l => [OverlaySpec] -> l -> l uncover os = liftL $ \l -> TeXComm "uncover" [ MSymArg $ fmap (TeXRaw . render) os , FixArg l ] +-- TODO: What is the difference between 'uncover' and 'only'?? + -- | Similar to 'uncover'. only :: LaTeXC l => [OverlaySpec] -> l -> l only os = liftL $ \l -> TeXComm "only" [ MSymArg $ fmap (TeXRaw . render) os , FixArg l ] +-- | Specifications for beamer functions. data OverlaySpec = OneSlide Int | FromSlide Int
Text/LaTeX/Packages/Graphicx.hs view
@@ -60,11 +60,11 @@ instance Render IGOption where render (IGWidth m) = "width=" <> render m render (IGHeight m) = "height=" <> render m - render (KeepAspectRatio b) = "keepaspectratio=" <> (fromString $ fmap toLower $ show b) + render (KeepAspectRatio b) = "keepaspectratio=" <> fromString (fmap toLower $ show b) render (IGScale r) = "scale=" <> render r render (IGAngle a) = "angle=" <> render a render (IGTrim l b r t) = "trim=" <> renderChars ' ' [l,b,r,t] - render (IGClip b) = "clip=" <> (fromString $ fmap toLower $ show b) + render (IGClip b) = "clip=" <> fromString (fmap toLower $ show b) render (IGPage p) = "page=" <> render p -- | Include an image in the document.
license view
@@ -1,4 +1,4 @@-Copyright (c)2012, Daniel Díaz +Copyright (c)2013, Daniel Díaz All rights reserved.