https://wiki.haskell.org/index.php?title=HaTeX_User's_Guide&feed=atom&action=historyHaTeX User's Guide - Revision history2016-05-29T23:09:51ZRevision history for this page on the wikiMediaWiki 1.19.14+dfsg-1https://wiki.haskell.org/index.php?title=HaTeX_User%27s_Guide&diff=58077&oldid=prevDaniel Díaz at 09:52, 14 May 20142014-05-14T09:52:01Z<p></p>
<table class='diff diff-contentalign-left'>
<tr valign='top'>
<td colspan='1' style="background-color: white; color:black;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black;">Revision as of 09:52, 14 May 2014</td>
</tr></table>Daniel Díazhttps://wiki.haskell.org/index.php?title=HaTeX_User%27s_Guide&diff=56835&oldid=prevDaniel Díaz: Update2013-09-15T16:47:46Z<p>Update</p>
<table class='diff diff-contentalign-left'>
<tr valign='top'>
<td colspan='1' style="background-color: white; color:black;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black;">Revision as of 16:47, 15 September 2013</td>
</tr></table>Daniel Díazhttps://wiki.haskell.org/index.php?title=HaTeX_User%27s_Guide&diff=45494&oldid=prevDaniel Díaz: Updated.2012-04-30T00:33:54Z<p>Updated.</p>
<table class='diff diff-contentalign-left'>
<tr valign='top'>
<td colspan='1' style="background-color: white; color:black;">← Older revision</td>
<td colspan='1' style="background-color: white; color:black;">Revision as of 00:33, 30 April 2012</td>
</tr></table>Daniel Díazhttps://wiki.haskell.org/index.php?title=HaTeX_User%27s_Guide&diff=45490&oldid=prevDaniel Díaz: First (and incomplete) edition. Testing the wiki backend of the guide.2012-04-28T17:08:29Z<p>First (and incomplete) edition. Testing the wiki backend of the guide.</p>
<p><b>New page</b></p><div>==Preface==<br />
<br />
===Introduction===<br />
<br />
If you are here because you want to learn more about HaTeX, or just feel curious, you are in the right place. First of all, note that this guide is addressed to that people that already knows the basics of both Haskell and LaTeX. Otherwise, try to learn first a bit of these languages (both are quite useful learnings). To learn Haskell, though I guess you already learned it since you are reading these lines, go to the Haskell web [http://haskell.org] and search for some tutorials or books. To learn LaTeX, you can start with''The not so short introduction to LaTeX'' [http://tobi.oetiker.ch/lshort/lshort.pdf].<br />
<br />
The HaTeX library aspires to be the tool that Haskellers could want to make theirLaTeX things without exit of their language (we understand that is difficult to leave Haskell after the first date), trying to be the most comprehensive and well done as possible. Do you think, anyway, that something could be done better? Perhaps something is lacked? Go then to the HaTeX mailing list [http://projects.haskell.org/cgi-bin/mailman/listinfo/hatex] and leave your complain without mercy! Or, in the case you are a GitHub user, say your word in the issue list [https://github.com/Daniel-Diaz/HaTeX/issues] or, to be awesome, make yourself a patch and send a pull request. This is the great thing about open source projects!<br />
<br />
===What is HaTeX?===<br />
<br />
Before we explain ''how'' HaTeX works, it is convenient to say ''what'' actually HaTeX is.<br />
<br />
''HaTeX is a Haskell library that provides functions to create, manipulate and parse LaTeX code.''<br />
<br />
People often says that ''HaTeX is a LaTeX DSL''. With it you can enjoy all the advantages you already have in Haskell while creating LaTeX documents. A common purpose is to automatize the creation of such documents, perhaps from a source data in Haskell. A more exotic one is to render chess tables. Possibilities are in a wide range. The idea is the following: if you can do it with LaTeX, you can do it with HaTeX, but adding all the Haskell features.<br />
<br />
==Basics==<br />
<br />
Through this section you will learn the basics of HaTeX. Essentially, ''how'' it works.<br />
<br />
===The Monoid class===<br />
<br />
If you are already familiar with the <hask>Monoid</hask> class, jump to the next point. The <hask>Monoid</hask> class is something that you must get used to in Haskell. But don't worry, it is quite simple (in spite of the similarity in the name with the <hask>Monad</hask> class). A ''monoid'' in Mathematics is an algebraic structure consisting of a set of objects with an operation between them, being this operation ''associative'' and with a ''neutral element''. Phew! But what is the meaning of this? By ''associative'' we mean that, if you have three elements<math>a</math>, <math>b</math> and <math>c</math>, then <math>a*(b*c) = (a*b)*c</math>. A ''neutral element'' is the one that does not worth to operate with, because it does nothing! To say, <math>e</math> is a ''neutral element'' if <math>e*a=a*e=a</math>, given any object <math>a</math>. As an example, you may take the ''real numbers'' as objects and the ordinary multiplication as operation.<br />
<br />
Now that you know the math basics behind the <hask>Monoid</hask> class, let's see its definition:<haskell><br />
class Monoid m where<br />
mempty :: m<br />
mappend :: m -> m -> m<br />
mconcat :: [m] -> m<br />
</haskell> See that <hask>mappend</hask> corresponds to the monoid operation and <hask>mempty</hask> to its neutral element. The names of the methods may seem insuitable, but they correspond to an example of monoid: the lists with the appending <hask>(++)</hask> operation. Who is the neutral element here? The empty list:<haskell><br />
xs ++ [] = [] ++ xs = xs<br />
</haskell> This class plays a significant role in HaTeX. Keep reading.<br />
<br />
===LaTeX blocks===<br />
<br />
Suppose we have a well-formed[[#Footnotes|<sup>1</sup>]] piece of LaTeX code, call it <math>a</math>. Now, let <hask>LaTeX</hask> be a Haskell type in which each element represents a well-formed piece of LaTeX code. Then, <math>a</math> can be seen as a Haskell expression <hask>a</hask> of type <hask>LaTeX</hask>. We can say that <hask>a</hask> is a <hask>LaTeX</hask> '''block'''. What happens if we append, by juxtaposition, two <hask>LaTeX</hask> blocks? As both are well-formed, so is the result. Thus, two blocks appended form another block. This way, we can define an operation over the <hask>LaTeX</hask> blocks. If we consider that a totally empty code is a well-formed piece of LaTeX code, we can speak about the empty block. And, as the reader may notice, these blocks with its appending form a monoid. Namely, <hask>LaTeX</hask> can be done an instance of the <hask>Monoid</hask> class.<br />
<br />
Of course, our mission using HaTeX is to create a <hask>LaTeX</hask> block that fits our purpose. The way to achieve this is to create a multitude of <hask>LaTeX</hask> blocks and, then, use the <hask>Monoid</hask> operation to collapse them all in a single block.<br />
<br />
===Creating blocks===<br />
<br />
We have now an universe of blocks forming a monoid. What we need now is a way to create these blocks. As we said, a block is the representation of a well-formed piece of LaTeX code. Let <hask>a</hask> be the block of the LaTeX expression <hask>\delta{}</hask>[[#Footnotes|<sup>2</sup>]]. Since this is a constant expression, it has a constant value in Haskell, named <hask>delta</hask>. Calling this value will generate the desired block.<br />
<br />
Other LaTeX expressions depend on a given argument. For example <hask>\linespread{x}</hask>, where <hask>x</hask> is a number. How we deal with this? As you expect, with functions. We can create blocks that depend on values with functions that take these values as arguments, where these arguments can be blocks as well. For instance, we have the function <hask>linespread</hask> with type:<haskell><br />
linespread :: Float -> LaTeX<br />
</haskell> As you may know, a title in LaTeX can contain itself LaTeX code. So the type for the Haskell function <hask>title</hask> is:<haskell><br />
title :: LaTeX -> LaTeX<br />
</haskell> And this is, essentialy, the way to work with HaTeX: '''to create blocks and combine them'''. Once you have your final block ready, you will be able to create its corresponding LaTeX code (we will see how later). Note that for every block there is a LaTeX code, but not for every code there is a block, because a malformed (in the sense of the negation of our well-formed concept) code has '''not''' a block in correspondence. This fact has a practical consequence: '''we cannot create malformed LaTeX code'''. ''And that's a good deal!''<br />
<br />
====From strings====<br />
<br />
Inserting text in a LaTeX document is a constant task. You can create a block with text given an arbitrary <hask>String</hask> with the <hask>fromString</hask> function, method of the <hask>IsString</hask> class:<haskell><br />
class IsString a where<br />
fromString :: String -> a<br />
</haskell> Since there is a set of characters reserved to create commands or another constructions,HaTeX takes care and avoids them replacing each reserved character with a command which output looks like the original character. For example, the backslash <hask>\</hask> is replaced with the <hask>\backslash{}</hask> command.<br />
<br />
The function that avoids reserved characteres is exported with the name <hask>protectString</hask>. Also, there is a variant for <hask>Text</hask> values called <hask>protectText</hask>.<br />
<br />
The use of the <hask>IsString</hask> class is because the ''Overloaded Strings'' extension. This one is similar to the ''Overloaded Numbers'' Haskell feature, which translates the number<hask>4</hask> to <hask>fromInteger 4</hask>. In a similar way, with <hask>OverloadedStrings</hask> enabled, the string<hask>"foo"</hask> is translated to <hask>fromString "foo"</hask>. If we now apply this to our blocks, the string <hask>"foo"</hask> will be automatically translated to a LaTeX block with ''foo'' as content. Quite handy! We will assume the <hask>OverloadedStrings</hask> extension enabled from now.<br />
<br />
====More blocks====<br />
<br />
There is a lot of functions for create blocks. In fact, we can say that this is the main purpose of the library. LaTeX has a lot of commands, in order to set font attributes, create tables, insert graphics, include mathematical symbols, ... So HaTeX have a function for each command defined in LaTeX (to tell the truth, only for a small subset). Please, go to the API documentation to read about particular functions.<br />
<br />
===Putting blocks together===<br />
<br />
Once you have the blocks, as we said before, you need to append them. The <hask>mappend</hask> method of the <hask>Monoid</hask> class does this work. If <hask>a</hask> and <hask>b</hask> are two blocks,<hask>mappend a b</hask>, or <hask>a `mappend` b</hask>, or even <hask>a <> b</hask>[[#Footnotes|<sup>3</sup>]], is the block with<hask>a</hask> and <hask>b</hask> juxtaposed. For long lists of blocks, you can try it with <hask>mconcat</hask> as follows:<haskell><br />
mconcat [ "I can see a " , textbf "rainbow"<br />
, " in the blue " , textit "sky" , "." ]<br />
</haskell><br />
<br />
===Rendering===<br />
<br />
This is the last step in our LaTeX document creation. When we have our finalLaTeX block <hask>a</hask>, the function <hask>renderFile</hask> can output it into a file, in the form of its correspondent LaTeX code.<br />
<br />
Say we have the next definition:<haskell><br />
short =<br />
documentclass [] article<br />
<> title "A short message"<br />
<> author "John Short"<br />
<> document (maketitle <> "This is all.")<br />
</haskell> Then, after call <hask>renderFile "short.tex" short</hask> it appears the following file in the current working directory (line formatting added for easier visualization):<haskell><br />
\documentclass{article}<br />
\title{A short message}<br />
\author{John Short}<br />
\begin{document}<br />
\maketitle{}<br />
This is all<br />
\end{document}<br />
</haskell> The function <hask>renderFile</hask> is not only for <hask>LaTeX</hask> values. Let's see its type:<haskell><br />
renderFile :: Render a => FilePath -> a -> IO ()<br />
</haskell> The <hask>Render</hask> class that appears in the context is defined:<haskell><br />
class Render a where<br />
render :: a -> Text<br />
</haskell> So, it is the class of types that can be rendered to a <hask>Text</hask> value. The type <hask>LaTeX</hask> is an instance, but other types, like <hask>Int</hask> or <hask>Float</hask>, so are too. These instances are useful for creating blocks from other values. With the function<hask>rendertex</hask>, any value in the <hask>Render</hask> class can be transformed to a block. First, the value is converted to <hask>Text</hask>, and then to <hask>LaTeX</hask> the same way we did with strings. But, '''be careful!''' Because <hask>rendertex</hask> does '''not''' escape reserved characters.<br />
<br />
===Try yourself===<br />
<br />
As always, the best way to learn something well is to try it by yourself. Since to see code examples can give you a great help, HaTeX comes with several examples where you can see by yourself how to get the work done.<br />
<br />
The API reference is also a good point to keep in mind. Descriptions of functions make you know how exactly they works. And, when they are not present, function names with type signatures may be a very helpful and descriptive.<br />
<br />
==Footnotes==<br />
<br />
<sup>1</sup>: With ''well-formed'' we mean that all braces, environments, math expressions, ... are closed.<br />
<br />
<sup>2</sup>: Please, note that the <hask>LaTeX</hask> block is '''not''' the same that the LaTeX expression. The former<br />
is a Haskell value, not the LaTeX code itself.<br />
<br />
<sup>3</sup>: From '''GHC 7.4''', <hask>(<>)</hask> is defined as a synonym for <hask>mappend</hask>. For previous<br />
versions of GHC, HaTeX exports the synonym.</div>Daniel Díaz