Posts Tagged ‘ latex ’

## Precompile pgfplots using tikzexternalize

Using pgfplots allows you to quickly create very beautiful plots, that look like they belong to your paper, by automatically using the correct fonts and styles. It does this by using the latex compiler to make the figures. This approach is very powerful but has a major drawback: the latex compiler was not made to do this. As a result, latex tends to run out of memory very fast, and projects with a lot of pgfplots tend to take a long time to compile.

We’ve previously shown how to increase the amount of memory latex has available, but this only solves half the problem.

A proper way to fix this is using the tikzexternalize command. It causes latex to compile the figures in a separate run, generating pdfs. These pdfs are then included in the main document, reducing both the memory requirements and the compilation time.

Using tikzexternalize is very simple, two step process

1. Call latex with the “shell-escape” option.
This looks like this on linux:

`pdflatex -shell-escape mydocument.tex`

The easiest way to do this is to modify the build command of your latex-editor of choice.

```\usepgfplotslibrary{external}
\tikzexternalize[prefix=TikzPictures/]
```

The first line loads the tikzexternalize library, the second line activates it. The second line also contains an option that specifies the folder where the resulting pdfs and intermediary files should be stored.

Latex will now externalize any tikzpicture you make. You can optionally use

`\tikzsetnextfilename{name_of_resulting_pdf}`

to specify a name for the resulting pdf file. This is strongly recommended, because latex will only recompile a figure if you remove the resulting pdf.

## Fix matlab and latex file associations on KDE

KDE (and linux) in general has a very elaborate system of guessing the file type of your file, that depends on both the file extension and the file contents. In 99% of the cases these rules work, unfortunately the other 1% has been annoying me for quite some time.
If you create a matlab m-file that starts with comments (% some comment), KDE will think the file is a latex file and insist on opening it with a dedicated latex editor.
This behaviour can be changed by modifying the mime type definitions.
Open `/usr/share/mime/packages/freedesktop.org.xml` in your favourite text editor as root.
Find the node `<mime-type type="text/x-tex">`. This will have a subnode that looks like:

```<magic priority="10">
<match value="%" type="string" offset="0"/>
</magic>
```

Change the priority to something smaller than 10, so it has a lower priority than the matlab magic rule.
Now run

```sudo update-mime-database /usr/share/mime
```

And that’s it. KDE will now see all your m-files as matlab scripts even if they start with a comment.

## Import latex formulas into inkscape

Getting nice looking formulas in your presentations / posters is always a problem. Latex is great at producing nice looking formulas and text, but for presentations and posters it’s not really equipped.

You could however write your formulas in a latex document, then import the pdf into inkscape and cut out the formulas. Unfortunately, inkscape will insist on treating your formulas as text, and mess them up in the process.
With a minor sidestep to acrobat professional we can get around this in five easy steps:

1. Open the pdf in acrobat professional.
2. Add a watermark using Document -> Watermark -> add. Set its opacity to 0%
3. Open advanced -> Printing Production -> Flattener Preview
4. Check “convert text to paths” and hit apply

The text has now been converted to a vector drawing, and can now be imported into inkscape without problems.
If you don’t add the watermark in acrobat professional, it’ll just ignore the “convert text to paths” option (don’t ask why). This trick can be used to import any fancy formatted text without running into font problems. (You do lose the ability to edit the actual text though)

## Extending latex memory

Latex is a very powerful typesetting application. A powerful package to be used with this is pgfplots, which allows you to render your plots using the latex engine. Unfortunately the default latex settings assume you’re running latex on an ancient 20 MHz 486 with less ram than your average mobile phone. This is ok if you’re just asking it to process text, but if you’re using pgfplots it will run out of memory trying to draw your images.

The solution is to increase the amount of memory latex uses.

Windows – Miktex
We’re doing this in the windows command line. You can get to this by opening the start menu and do `run ...`. Type `cmd` to start the terminal. On vista and up, you can just type `cmd` in the search field.

1. Go the folder where Miktex is installed. Most likely this is `C:\Program Files\Miktex 2.7\`
2. Go to the subfolder `Miktex`
3. Go to the subfolder `bin`
4. run
```initexmf --edit-config-file=pdflatex
```

Replace `pdflatex` with `latex` or `xetex` if you’re using that.

5. You’ll get a notepad screen with a file called `pdflatex.ini`. Add the line
```main_memory=2000000
```

Don’t worry about the exact number, just make it big.

6. Save the file
7. run
```initexmf --dump=pdflatex
```
8. That’s it. Enjoy your nice pgfplots figures

Linux – Texlive
Do the following things in a terminal as root.

1. Find out where your `texmf.cnf` is located
```kpsewhich texmf.cnf
```

This will most likely be `/usr/share/texmf/web2c/texmf.cnf`

2. Open it and search for main_memory line and modify it to
```main_memory=2000000
```
3. Save the file
4. run
```fmtutil-sys --all
```