HaTeX- The Haskell LaTeX library.

Safe HaskellSafe




LaTeX syntax description in the definition of the LaTeX datatype. If you want to add new commands or environments not defined in the library, import this module and use LaTeX data constructors.


LaTeX datatype

data Measure Source #

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.


Pt Double

A point is 1/72.27 inch, that means about 0.0138 inch or 0.3515 mm.

Mm Double


Cm Double


In Double


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.

data MathType Source #

Different types of syntax for mathematical expressions.



data LaTeX Source #

Type of LaTeX blocks.


TeXRaw Text

Raw text.

TeXComm String [TeXArg]

Constructor for commands. First argument is the name of the command. Second, its arguments.

TeXCommS String

Constructor for commands with no arguments. When rendering, no space or {} will be added at the end.

TeXEnv String [TeXArg] LaTeX

Constructor for environments. First argument is the name of the environment. Second, its arguments. Third, its content.

TeXMath MathType LaTeX

Mathematical expressions.

TeXLineBreak (Maybe Measure) Bool

Line break command.

TeXBraces LaTeX

A expression between braces.

TeXComment Text


TeXSeq LaTeX LaTeX

Sequencing of LaTeX expressions. Use <> preferably.


An empty block. Neutral element of <>.


Eq LaTeX Source # 


(==) :: LaTeX -> LaTeX -> Bool #

(/=) :: LaTeX -> LaTeX -> Bool #

Show LaTeX Source # 


showsPrec :: Int -> LaTeX -> ShowS #

show :: LaTeX -> String #

showList :: [LaTeX] -> ShowS #

IsString LaTeX Source #

Method fromString escapes LaTeX reserved characters using protectString.


fromString :: String -> LaTeX #

Monoid LaTeX Source #

Method mappend is strict in both arguments (except in the case when the first argument is TeXEmpty).


mempty :: LaTeX #

mappend :: LaTeX -> LaTeX -> LaTeX #

mconcat :: [LaTeX] -> LaTeX #

Arbitrary LaTeX Source # 


arbitrary :: Gen LaTeX #

shrink :: LaTeX -> [LaTeX] #

LaTeXC LaTeX Source #

This instance just sets liftListL = id.


liftListL :: ([LaTeX] -> LaTeX) -> [LaTeX] -> LaTeX Source #

Render LaTeX Source # 


render :: LaTeX -> Text Source #

Texy LaTeX Source # 


texy :: LaTeXC l => LaTeX -> l Source #

data TeXArg Source #

An argument for a LaTeX command or environment.


FixArg LaTeX

Fixed argument.

OptArg LaTeX

Optional argument.

MOptArg [LaTeX]

Multiple optional argument.

SymArg LaTeX

An argument enclosed between < and >.

MSymArg [LaTeX]

Version of SymArg with multiple options.

ParArg LaTeX

An argument enclosed between ( and ).

MParArg [LaTeX]

Version of ParArg with multiple options.

(<>) :: Monoid m => m -> m -> m infixr 6 #

An infix synonym for mappend.


Escaping reserved characters

protectString :: String -> String Source #

Escape LaTeX reserved characters in a String.

protectText :: Text -> Text Source #

Escape LaTeX reserved characters in a Text.

Syntax analysis

matchCommand :: (String -> Bool) -> LaTeX -> [(String, [TeXArg])] Source #

Traverse a LaTeX syntax tree and returns the commands (see TeXComm and TeXCommS) that matches the condition and their arguments in each call.

lookForCommand Source #


:: String

Name of the command.

-> LaTeX

LaTeX syntax tree.

-> [[TeXArg]]

List of arguments passed to the command.

Look into a LaTeX syntax tree to find any call to the command with the given name. It returns a list of arguments with which this command is called.

lookForCommand = (fmap snd .) . matchCommand . (==)

If the returned list is empty, the command was not found. However, if the list contains empty lists, those are callings to the command with no arguments.

For example

lookForCommand "author" l

would look for the argument passed to the \author command in l.

matchEnv :: (String -> Bool) -> LaTeX -> [(String, [TeXArg], LaTeX)] Source #

Traverse a LaTeX syntax tree and returns the environments (see TeXEnv) that matches the condition, their arguments and their content in each call.

lookForEnv :: String -> LaTeX -> [([TeXArg], LaTeX)] Source #

Similar to lookForCommand, but applied to environments. It returns a list with arguments passed and content of the environment in each call.

lookForEnv = (fmap (\(_,as,l) -> (as,l)) .) . matchEnv . (==)

texmap Source #


:: (LaTeX -> Bool)


-> (LaTeX -> LaTeX)

Function to apply when the condition matches.

-> LaTeX 
-> LaTeX 

The function texmap looks for subexpressions that match a given condition and applies a function to them.

texmap c f = runIdentity . texmapM c (pure . f)

texmapM Source #


:: (Applicative m, Monad m) 
=> (LaTeX -> Bool)


-> (LaTeX -> m LaTeX)

Function to apply when the condition matches.

-> LaTeX 
-> m LaTeX 

Version of texmap where the function returns values in a Monad.


getBody :: LaTeX -> Maybe LaTeX Source #

Extract the content of the document environment, if present.

getPreamble :: LaTeX -> LaTeX Source #

Extract the preamble of a LaTeX document (everything before the document environment). It could be empty.