@@ -27,7 +27,7 @@ Without further ado, in alphabetical order, here are 10 useful LaTeX packages fo

### adjustbox

The `adjustbox` package {% cite Scharrer2012 --file latex-packages %} allows scaling of a figure or plot, for example one made in TikZ.

The `adjustbox` package {% cite Scharrer2012 %} allows scaling of a figure or plot, for example one made in TikZ.

{% highlight tex linenos %}

\usepackage{adjustbox} % Balances the columns of the last page

...

...

@@ -48,7 +48,7 @@ Here is an example from a real paper, slightly edited for clarity.

### balance or flushend

The two columns on the last page of a two-column paper should be the same length. This package saves you from having to balance the columns manually {% cite Daly1999 --file latex-packages %}.

The two columns on the last page of a two-column paper should be the same length. This package saves you from having to balance the columns manually {% cite Daly1999 %}.

{% highlight tex linenos %}

\usepackage{balance} % Balances the columns of the last page

...

...

@@ -82,7 +82,7 @@ Simply add `\balance` in the first column of the last page, and you're done! Som

### booktabs

Simply put, `booktabs` {% cite Fear2020 --file latex-packages %} makes your tables look good!

Simply put, `booktabs` {% cite Fear2020 %} makes your tables look good!

Consider this example, adapted from [File:LaTeX animal table with booktabs.png](https://commons.wikimedia.org/w/index.php?title=File:LaTeX_animal_table_with_booktabs.png&oldid=287782285)(licensed under the [Creative Commons Attribution 4.0 International](https://creativecommons.org/licenses/by/4.0/deed.en) license).

...

...

@@ -122,7 +122,7 @@ Armadillo & frozen & 8.99 \\

### cleveref

Using the `cleveref` package {% cite Cubitt2012 --file latex-packages %}, we can refer to figure, tables etc without specificing the type.

Using the `cleveref` package {% cite Cubitt2012 %}, we can refer to figure, tables etc without specificing the type.

`See~\cref{tbl:mytable}` produces "See table 1".

{% highlight tex linenos %}

...

...

@@ -133,7 +133,7 @@ The result is configurable, and we can for example change the figure type name w

### glossaries-extra

Using the package `glossaries-extra` we can add a list of terms and a list of acronyms to our document {% cite Talbot2016 Talbot2018 --file latex-packages %}. It is also very useful for making sure that acronyms are written out the first time they are used.

Using the package `glossaries-extra` we can add a list of terms and a list of acronyms to our document {% cite Talbot2016 Talbot2018 %}. It is also very useful for making sure that acronyms are written out the first time they are used.

{% highlight tex linenos %}

\usepackage[acronym]{glossaries-extra}

...

...

@@ -147,7 +147,7 @@ We can then use `\gls{gcd}` whenever we need to use this acronym. See [Glossarie

### microtype

Text typeset in LaTeX already looks great, but can be made even nicer with `microtype` {% cite Schlicht2019 --file latex-packages %}. Looking deeper at the micro-typographics extensions provided by `microtype` is a great way to ensure getting trapped in a rabbit hole for a long time. Here are the settings I use, borrowed from the excellent post [Tips on Writing a Thesis in LaTeX](http://www.khirevich.com/latex/microtype/) {% cite Khirevich %}.

Text typeset in LaTeX already looks great, but can be made even nicer with `microtype` {% cite Schlicht2019 %}. Looking deeper at the micro-typographics extensions provided by `microtype` is a great way to ensure getting trapped in a rabbit hole for a long time. Here are the settings I use, borrowed from the excellent post [Tips on Writing a Thesis in LaTeX](http://www.khirevich.com/latex/microtype/) {% cite Khirevich %}.

{% highlight tex linenos %}

\usepackage[

...

...

@@ -187,11 +187,11 @@ Text typeset in LaTeX already looks great, but can be made even nicer with `micr

{% include figure.html url="/images/pgfplot.png" description="Example figure produced with this method." %}

See [Publication ready figures]({% post_url 2019-09-29-publication_ready_figures %}) for a longer post on the pgfplots package {% cite Feuersanger2014 --file latex-packages %}.

See [Publication ready figures]({% post_url 2019-09-29-publication_ready_figures %}) for a longer post on the pgfplots package {% cite Feuersanger2014 %}.

### savetrees

`savetrees` {% cite Pakin2007 --file latex-packages %} is magic. It is highly useful when you are approaching a conference deadline and you have one page over the conference limit. Using the `subtle` mode, we can preserve the document layout and try to make LaTeX pack text tighter.

`savetrees` {% cite Pakin2007 %} is magic. It is highly useful when you are approaching a conference deadline and you have one page over the conference limit. Using the `subtle` mode, we can preserve the document layout and try to make LaTeX pack text tighter.

{% highlight tex linenos %}

\usepackage[subtle]{savetrees}

...

...

@@ -225,7 +225,7 @@ See [Publication ready figures]({% post_url 2019-09-29-publication_ready_figures

### siunitx

Typesetting numbers with units is actually not trivial. `siunitx` {% cite Wright2009 --file latex-packages %} adds, among others, the commands `\SI` and `\num` so that we can write `\SI{1024}{\byte}` or <code>\num{{}e5}</code>, which makes things look much nicer.

Typesetting numbers with units is actually not trivial. `siunitx` {% cite Wright2009 %} adds, among others, the commands `\SI` and `\num` so that we can write `\SI{1024}{\byte}` or <code>\num{{}e5}</code>, which makes things look much nicer.

{% highlight tex linenos %}

\usepackage{siunitx} % A better way of writing things with units

...

...

@@ -235,7 +235,7 @@ Typesetting numbers with units is actually not trivial. `siunitx` {% cite Wright

### tikz

TikZ and PGF {% cite Tikz2014 --file latex-packages %} are packages for programatically creating figures, and deserve a longer writeup.

TikZ and PGF {% cite Tikz2014 %} are packages for programatically creating figures, and deserve a longer writeup.

@@ -63,19 +63,19 @@ At submission time we find ourselves fighting the automated PDF checks of the pu

</div>

</div>

One large step towards a solution to this was proposed by {% cite Acher2018 --file latex-packages %} in which the authors annotate the LaTeX source with variability information. This information can be numerical values on figure sizes, or boolean values on options or whether to include certain paragraphs. In their work they formulate the learning problem as a constrained binary classification problem to classify into acceptable and non-acceptable configurations so that acceptable solutions can be presented to the user.

One large step towards a solution to this was proposed by {% cite Acher2018 %} in which the authors annotate the LaTeX source with variability information. This information can be numerical values on figure sizes, or boolean values on options or whether to include certain paragraphs. In their work they formulate the learning problem as a constrained binary classification problem to classify into acceptable and non-acceptable configurations so that acceptable solutions can be presented to the user.

Here, we instead formulate this problem as a constrained optimization problem, where the constraints are defined by the automated PDF checks and the optimization is defined by proxy metrics such as amount of white space on the last page.

To find the optimal variable values we will use Ray Tune {% cite liaw2018tune --file python-packages %}. Ray Tune allows us to run parallel, distributed, LaTeX compilations and provides a large selection of search algorithms and schedulers.

To find the optimal variable values we will use Ray Tune {% cite liaw2018tune %}. Ray Tune allows us to run parallel, distributed, LaTeX compilations and provides a large selection of search algorithms and schedulers.

### Contributions

This work is inspired by three papers and develop these foundations in the following ways:

- We define and implement a proxy-metric for paper quality, based on findings from {% cite Huang2018 %}, that can be directly measured on a PDF-file. See [Our LaTeX manuscript and a proxy quality metric](#our-latex-manuscript-and-a-proxy-quality-metric).

- We show how a LaTeX document can be annotated using variability information following ideas from {% cite Acher2018 --file latex-packages %} but without using a preprocessor. See [Annotating the LaTeX source code](#annotating-the latex-source-code).

- We formulate a constrained non-convex objective function and proceed to solve it efficiently using Ray Tune {% cite liaw2018tune --file python-packages %}. See [Optimization problem](#pptimization-problem) and [Hyperparameter search](#hyperparameter-search).

- We show how a LaTeX document can be annotated using variability information following ideas from {% cite Acher2018 %} but without using a preprocessor. See [Annotating the LaTeX source code](#annotating-the latex-source-code).

- We formulate a constrained non-convex objective function and proceed to solve it efficiently using Ray Tune {% cite liaw2018tune %}. See [Optimization problem](#pptimization-problem) and [Hyperparameter search](#hyperparameter-search).

## Methods

...

...

@@ -154,7 +154,7 @@ The basic algorithm is as follows: We loop over each

bounding box within the last page. For every bounding box we add an interval to

an _interval tree_ for the dimensions in the y-direction. We can the use this to find the difference between the page dimensions and the extent of the bounding boxes on the page. For this we will use the Python package

The implementation is discussed more in [How to beat publisher PDF checks with LaTeX document unit testing]({% post_url 2020-05-16-latex-test-cases %}).

...

...

@@ -229,7 +229,7 @@ Note that we cannot tune the hyper-parameter $$\lambda$$, so we set it to $$2\pi

#### Defining the tasks

We will use Ray Tune {% cite liaw2018tune --file python-packages %} for searching this parameter space (the parameters we are defining here are of course not hyper-parameters). We begin with an exhaustive grid-search over the entire search space which is here $$v \in [0, 1]\,\forall \theta_k \in \boldsymbol{\theta}$$. Since we have two figures each with a variable width we have $$\vert\boldsymbol{\theta}\vert = 2$$. Performing a grid-search over each of the variables divided into 51 possible values gives us 2601 paper variants to compile. Each of the paper compilations consists of five steps;

We will use Ray Tune {% cite liaw2018tune %} for searching this parameter space (the parameters we are defining here are of course not hyper-parameters). We begin with an exhaustive grid-search over the entire search space which is here $$v \in [0, 1]\,\forall \theta_k \in \boldsymbol{\theta}$$. Since we have two figures each with a variable width we have $$\vert\boldsymbol{\theta}\vert = 2$$. Performing a grid-search over each of the variables divided into 51 possible values gives us 2601 paper variants to compile. Each of the paper compilations consists of five steps;

1. Copy the LaTeX code to a temporary directory.

1. Sample variables and write to file `macros.tex`,

...

...

@@ -250,7 +250,7 @@ The average execution time for each task is 5.46 seconds, and we can run 8 of th

#### Search algorithm

However, running an exhaustive grid search is not needed as we can use one of the search algorithms provided by Ray Tune instead. Specifically we will use a [Asynchronous HyperBand](https://docs.ray.io/en/ray-0.4.0/hyperband.html) scheduler with a [HyperOpt](https://docs.ray.io/en/master/tune/api_docs/suggestion.html#tune-hyperopt) search algorithm. HyperOpt {% cite DBLP:conf/icml/BergstraYC13 --file python-packages %} is a Python library for optimization over awkward search spaces. In our use-case we have real-valued, discrete and conditional variables so this library works for us, but we will not evaluate its performance on our objective function and can therefore not claim that it is the best search algorithm for this particular problem.

However, running an exhaustive grid search is not needed as we can use one of the search algorithms provided by Ray Tune instead. Specifically we will use a [Asynchronous HyperBand](https://docs.ray.io/en/ray-0.4.0/hyperband.html) scheduler with a [HyperOpt](https://docs.ray.io/en/master/tune/api_docs/suggestion.html#tune-hyperopt) search algorithm. HyperOpt {% cite DBLP:conf/icml/BergstraYC13 %} is a Python library for optimization over awkward search spaces. In our use-case we have real-valued, discrete and conditional variables so this library works for us, but we will not evaluate its performance on our objective function and can therefore not claim that it is the best search algorithm for this particular problem.

#### Defining the experiment

...

...

@@ -296,7 +296,7 @@ See the complete source code in the [repository](https://gitlab.com/martisak/lat

### Track and visualize results

We can visualize the results of the hyper-parameter search using [Tensorboard](https://www.tensorflow.org/tensorboard) {% cite tensorflow2015 --file python-packages %} in a Docker {% cite Merkel2014 %} container using the following command:

We can visualize the results of the hyper-parameter search using [Tensorboard](https://www.tensorflow.org/tensorboard) {% cite tensorflow2015 %} in a Docker {% cite Merkel2014 %} container using the following command:

MLFlow {% cite MLFlow --file python-packages %} is a Python package that can be used to for example track experiments and models. Here we use it to visualize the hyper-parameters and the corresponding objective function score. The MLFLow UI can be started simply with <code>mlflow ui</code>.

MLFlow {% cite MLFlow %} is a Python package that can be used to for example track experiments and models. Here we use it to visualize the hyper-parameters and the corresponding objective function score. The MLFLow UI can be started simply with <code>mlflow ui</code>.

### Solutions found

...

...

@@ -362,7 +362,7 @@ Using Ray Tune has a major advantage in that it is easy to parallelize our docum

## Related work

In the inspiring "VaryLaTeX: Learning Paper Variants That Meet Constraints" paper {% cite Acher2018 --file latex-packages %} the authors annotate LaTeX source with variability information and construct a binary classification problem where the aim is to classify a configuration as fulfilling the constraints or not. The classifier can be used to present a set of configurations to the user that then can pick an aesthetically pleasing configuration out of the presented set. This is a much more complete solution that the one presented in this post. A potentially interesting addition to their work is to annotate the configurations with a score, that can be used to sort a potentially large set of valid configurations. See their [Github repository](https://github.com/FAMILIAR-project/varylatex).

In the inspiring "VaryLaTeX: Learning Paper Variants That Meet Constraints" paper {% cite Acher2018 %} the authors annotate LaTeX source with variability information and construct a binary classification problem where the aim is to classify a configuration as fulfilling the constraints or not. The classifier can be used to present a set of configurations to the user that then can pick an aesthetically pleasing configuration out of the presented set. This is a much more complete solution that the one presented in this post. A potentially interesting addition to their work is to annotate the configurations with a score, that can be used to sort a potentially large set of valid configurations. See their [Github repository](https://github.com/FAMILIAR-project/varylatex).

{% cite Huang2018 %} constructs a binary classification tasks to predict if a paper is good or bad, based on the Paper Gestalt of a paper, i.e. only the visual representation of the paper. The paper goes on to discuss how to improve the Paper Gestalt, for example adding a teaser figure, a figure on the last page and filling the complete last page. The latter can be numerically estimated, which is what we based this blog post on.

...

...

@@ -370,7 +370,7 @@ In the inspiring "VaryLaTeX: Learning Paper Variants That Meet Constraints" pape

The experiments in this blog post aim to produce a single paper that fulfills all publisher constrains and requirements. This single paper is optimized in terms of last page white space, which disregards pretty much everything that makes a paper great and should therefore be used with caution.

There are other metrics that we can use for optimization, for example the number of words, data density {% cite Tufte:1986:VDQ:33404 %}. We can add more variability by considering figure placement, <code>microtype</code> options {% cite Schlicht2019 --file latex-packages %}, and more.

There are other metrics that we can use for optimization, for example the number of words, data density {% cite Tufte:1986:VDQ:33404 %}. We can add more variability by considering figure placement, <code>microtype</code> options {% cite Schlicht2019 %}, and more.

The optimization can be added as a stage in a LaTeX pipeline as outlined in [How to annoy your co-authors: a Gitlab CI pipeline for LaTeX]({% post_url 2020-05-16-latex-test-cases %})