Fill Rules in TikZ

Filling shapes (or any path for that matter) in TikZ can be done with the \fill command. This requires very little thought on our part when it comes to filling simple shapes, e.g. circles or squares but it gets a little more complicated once we draw shapes with intersecting paths or holes. This is a difficult problem because we don't always know where the "inside" of a shape lies--this is not as simple as it sounds. To do this, we must understand the types of polygons there are as well as the rules for filling them with color.

Types of Polygons

We can divide polygons into two types: standard polygons and general polygons. Standard polygons are include circles, squares and triangles. These shapes have no holes and their paths don't self-intersect unlike general polygons.

The circle, square and triangle are basic polygons. Their paths do not intersect and the shape does not have a hole unlike the general polygon below.

Filling Shapes

There are two rules we can use to fill fhapes in TikZ:

• Even-Odd Parity Rule
• Non-zero Winding Rule

Both rules are algorithms that are used to determine whether a given point falls within an enclosed curve. To determine the "insideness" of a point, we draw a ray from the point we wish to determine to infinity in any direction. With the even-odd rule, we count the number of times the ray crosses a path segment. If the number is odd then the point lies on the inside of shape. If it is even then it lies on the outside.

Unlike the even-odd rule, the non-zero winding rule relies on knowing the stroke direction for each part the ray crosses. The number of intersections is scored and given a winding number. If the path is clockwise, or runs left to right, we subtract 1 from the score. If the path is counter-clockwise, or runs from right to left, we add one to the score. If the total winding number is zero then our point lies on the outside but if the winding number is not zero then the point lies on the inside. We will see how both rules differ and why.

Even-Odd Parity Rule

We construct rays, both inside and outside, going through our shape of interest to determine if a point lies on the inside or ourside.

For a standard polygon, the shapes shown on top, it is easy to see where the outside lies but let's construct a few rays (shown in black) that lie outside and see what we get. For the circle, square, and triangle, the ray crosses the shape's path twice. This sum is even and we can conclude that the point lies on the outside of those shapes.

Now we construct some rays (shown in red) that lie on the inside. We see that they all cross the shape's path once. This makes the sum of path crossings odd and we conclude that the point lies on the inside of the shape.

But what happens with the general polygon below? We see that the black ray crosses the general polygon's path four times--an odd sum. This point lies on the outside of the shape. When we look at the red ray we see it crosses the shape's path once and we conclude that point lies on the inside.

Now let's look at the blue ray as it goes out from the hole. There is some doubt whether that point lies on the inside or outside of this shape but we can apply the even-odd rule. We see that the blue ray crosses the shape's path two times, an even number of times, which means the point lies on the outside of the shape and we don't color this area.

Filling shapes with the Even-Odd Parity Rule

So we see how we can implement the even-odd rule in TikZ and how it works. But how exactly does the Non-Zero Winding Rule differ and will the results be the same? As you have probably guessed, the answer is no. After all, why have two different rules that do the exact same thing?

Non-Zero Winding Rule

With the non-zero winding rule we need to consider the direction the path goes.

The non-zero winding rule is different from the even-odd rule in that we must consider the direction the path takes as the ray crosses it. We draw the direction of the winding path so we can determine whether a point lies on the inside or outside of a shape.

We use our rays to determine whether a point lies on the inside or outside of a shape.

We do the same thing with the Even-Odd Parity Rule with one exception--if the path runs counter-clockwise as we cross it, we add one and subtract one if it runs clockwise.

For the circle, the first path crossing runs from left-to-right so we subtract one. As we continue our ray, the next crossing runs from right-to-left and we add one. The sum of our path crossings is zero and we conclude, like in the case of the even-odd rule, the point lies on the outside.

We see that for the circle, square, and triangle, the results are the same for the even-odd rule. Things are a little different for the general shape seen below. While the red and black rays yield the same result, the blue ray gives us something very different. The first crossing of the blue ray goes from left-to-right, we subtract one. The next crossing also goes from left-to-right, we again subtract one. The sum of our crossings is -2. The point lies on the inside and we fill the "hole" with color.

We see the different effect of the Non-Zero Winding Rule on general polygons.

Path Matters

In the case of general polygons, the even-odd parity rule (shown left) gives a different result from the non-zero winding rule (shown right).

We see the even-odd parity rule and the non-zero winding rule give different results for general polygons. This doesn't mean this is always the case. Path direction in the non-zero winding rule is the determining factor for the results we get.

We can create a general polygon with a hole where one shape lies inside the other and where their paths do not intersect. We can use TikZ code to specify the path directions of the innermost and outermost shapes. On the left, the outermost square's path curves around in a clockwise direction but its innermost square curls in a counterclockwise direction. We see with the general polygon on the right, both the inner and outer squares both curl clockwise.

We can construct a general polygon with non-continuous paths. The difference is that the left inner square's path is counter-clockwise while the one on the right rotates in the clockwise direction.

We know that the even-odd rule will leave the hole unfilled in both cases as we are just counting path crossings but if we were to use the non-zero rule, on both shapes? We see that the outcome depends on path direction. The shape on the left with a counter-clockwise inner square produces the same result as the even-odd rule but the hole is filled when the inner path goes in a clockwise direction.

The non-zero winding rule can produce different results for the same shape. This is due to the path differences of the inner rectangle.

Conclusion

Filling shapes with color doesn't have to be a complex operation if you understand the two fill rules and how they work. Generally, the even-odd parity rule won't fill a hole but whether the non-zero winding rule does depends on the path direction of the hole. Once we understand that, drawing and coloring shapes in TikZ becomes very easy.

Aligning Numbers in siunitx

The use of tables to communicate quantitative information is common in both academia and business but effective design sometimes eludes us. Readers must, at a glance, be able to get the information they need from the data you present. Fortunately, the tools for effective table design exist in LaTeX at least, if you know what you are doing. We are going to look at some simple ways to align numbers using the siunitx package.

The Table

We have the following table. While it is fairly presentable, we can't help but wonder can we do better? Yes, we see the numbers are fairly aligned in most cases but things look a little out of place where there are negative numbers.

The numbers in the table are centered but the decimals are not aligned. We also need to italicize some of the numbers to highlight important data.

The code for our table looks something like this:

Though we can probably leave this table as is, we need to make some changes. Some of the numbers also need to be italicized to highlight some important data. This should be simple as we see below.

Some of the numbers have been italicized but this throws off some of the decimal alignment.

So the question is, how can we make our table look better?

The siunitx package

The siunitx package not only allows authors to typeset quantities with ease and in a consistent way but also provides the tools to typeset tables. The package introduces another column type--the S column--to control alignment. We can then use the package's number parser to align our numbers. We are also going to use the [table-format=2.6] to tell the package something about our number. In our case, the number 2.6 tells the parser that the number has two integer digits and six decimal places. There are other options we can use for our S-columns that are outlined in the siunitx manual.

The siunitx package allows for decimal alignment rather just centering numbers and hoping for the best.

which we can see from the code below.

Wait a second. Didn't we just say that the S-column allows us to align numbers? Well, it seems that some of the italicized numbers are no longer aligned and the dash is no longer centered. If anything, the siunitx package has made things worse! Does this mean we should go back to what we had before?

Fixing Decimal Alignment in siunitx

The problem we face is that the siunitx package's math parser doesn't see the italicized numbers as numbers but rather as text. It's the reason why the dash in the table is left aligned. Let's focus on each problem instead of worrying about both at the same time. Instead of the \textit macros, we will use \mathit to italicize numbers. This will tell siunitx's math parser its dealing with a number and not a word. Now our fix looks like:

The \mathit macro tells the siunitx package that it is dealing with a number and not a word.

and the code looks something like:

There, that looks much better but we still have to deal with the left aligned dash. Now we must tell the siunitx parser to center the dash. We can do this by enclosing the cell's contents in curly braces:

By enclosing the \multirow command in curly braces we tell the sinuitx package's parser to center the text.

and our code looks like:

Conclusion

And there you have it. By using the siunitx package you can easily align numbers and text in your tables. Now your tables look great you can get back to stress-free writing.

Using the pas-tableur style for spreadsheets in LaTeX

One of LaTeX's strengths is its ability to typeset publication quality tables using the booktabs but sometimes we need something that look like a spreadsheet. There are several solutions to this problem. If we are working in Excel, we can, for example, select the necessary cells of interest and create an image to be inserted into our document. There are some problems with this solution. As we are all too aware, things change during the course of writing and if our spreadsheet changes, we must go through the process of recreating our image and inserting the new image into our LaTeX code.

Though it takes some time and effort (in the beginning) there are several good reasons to rendering something that looks like a spreadsheet in code. For one, we can easily see the code to make sure everything is correct before we compile but the biggest advantage is that any changes can be made easily. We don't have to fire up Excel (shudder) and so save time during the editing process. As ideal as this solution is, there is one problem. A huge one. It takes some planning and prep for it to move smoothly.

To get an idea of how you can do this you can take a look at the "Typesetting a table to look like a spreadsheet" discussion on StackExchange. It's not difficult and we see one solution using the array and colortbl packages and another in TikZ. Of course, there is a problem of utility. These solutions are fine if you just need to show one spreadsheet image. What if you have many spreadsheets, for example, in the case where you're writing a book? How can you make this easier? You can use the TikZ pas-tableur package by Stéphane Pas­quet.

Getting Started

Though the pas-tableur package documentation is written in French, the package is easy to utilize. To start you just need to load the package:

The tableur command automatically creates a spreadsheet with the tableur command. As this uses the TikZ package, the command must be enclosed within the tikzpicture environment. The command syntax is:

To create a 3 by 4 spreadsheet, we use:

which looks like

A pas-tableur spreadsheet with no options defined.

Changing colors up

This isn't bad as things go but what if you need to customize things a bit? Turns out this is simple. You can define these options in the preamble.

The above are the default options. What is we wanted to change the color of the top part of the header to yellow?

which gives us the output:

A pas-tableur spreadsheey with the top part of the header colored in yellow.

and as Bermuda Sand is a wonderful color, we add the command:

and we get:

A pas-tableur spreadsheet with the top part of the header colored in yellow and the bottom part colored in Bermuda Sand.

Now that you're adjusted the header colors, what if you needed to change the cell lines to a nice dark golden color?

which looks like:

A pas-tableur spreadsheet with the color of the cell lines changed.

Changing cell size

Now that we have had fun with changing the colors of our spreadsheet, what if we just wanted to change the cell width and height? For that we turn to the colminwidth and lineminheight commands.

Our extra long spreadsheet looks like this:

Image showing a pas-tableur spreadsheet with a long cell width.

We can do the same thing for the cell height:

which looks like:

A pas-tableur spreadsheet with extra tall cells

Filling the cells

Drawing empty spreadsheets are fun but generally we fill spreadsheets with text and formulas. This can be done with the celtxr command.

Now we can fill our spreadsheet:

which looks like:

The pas-tableur spreadsheet has been filled with text and formulas.

Selecting and Highlighting Cells

Selecting a Single Cell

You may need to select or highlight cells to illustrate your point. For that, we turn to the selecCell command.

So if we wanted to select the C2 cell of our spreadsheet:

Selecting Multiple Cells

Sometimes we need to highlight, not one, but a group of cells. For that we turn to the multiSelec command:

So if we wanted to highlight B2 to D4 in our spreadsheet:

which looks like:

Here we can see the B2 and D4 cells highlighted.

Changing the colors

But what if you wanted to change the color of the highlighted headers? For that we define the blueSelecCellTop and the blueSelecCellBottom colors in the preamble:

which looks like:

Here we change the colors of the top and bottom parts of the highlighted cells.

Conclusion

The pas-tableur package is an easy to use LaTeX package for generating spreadsheet images. Of course, you can use your favorite spreadsheet, capture it as an image and integrate it into your document but the package offers some advantages. As everything is generated in LaTeX code, you can add spreadsheets and make edits easily--the process becomes a part of your writing which is something we all love about LaTeX.

I found the package easy to use and understand and would recommend it to anyone who needs to generate spreadsheet images into their document.

BibExport and Extracting Cited Bibliographies

There are lots of tools and software that make bibliography management easy in LaTeX. We can keep every bibliographic entry stored in one file -- a centralized bibliographic source -- that can be linked to as many documents as desired, or we can make use of several BibTeX files and access them as needed. Problems arise when the need to collaborate or publish arises. We certainly can't send our entire .bib file now, can we? We can manually edit and create a smaller .bib file to a collaborator or an editor but this can be both difficult and tedious. If only there was a simpler way. An automated way.

It turns out there is in the form of the bibexport tool -- a Bourne shell script tool that extracts bibliography entries that are cited in a document -- and its use is pretty simple. The tool also allows you to export the cited entries from more than one .bib file.

Extracting with and without cross-references

Extracting \cite'd references of a document with cross-references is simple:

It must be noted that you must first compile your document for the .aux file to be available. But what if we needed to extract references without cross references and using a BibTeX style (.bst) file. Well, that too is simple.

Extracting from more than one BibTeX file

Sometimes we have more than one bibliographic database or .bib file. Again, this helpful tool takes care of that problem.

Using it all

Now that all the hard work has been done -- no sarcasm here -- we can now include the new extracted .bib file in our document. And who said LaTeX has to be difficult?

Quotation Marks and the LaTeX dirtytalk package

When typesetting documents in LaTeX, users must be aware of the rules regarding quotations for different languages. In the English language, there are differences between the primary and secondary quotation marks used in US and UK English as we can see in our CTAN Quotation Packages in LaTeX blog article. To make things even more interesting, while the German language has its own set of quotation marks („ quote”) used in everyday writing, some modern printed books use a reverse order French quotation marks or « quillemets ».

While babel package does allow for the typesetting of quotation marks across different languages, see table below, and though it is possible to use either the appropriate key combination or TeX code, typing quotes can be cumbersome depending on the language.

Table of international quotation marks and their babel key combinations and LaTeX codes
Language Quotation Marks Key Combination LaTeX Code
Primary  Secondary Primary  Secondary  Primary  Secondary
English (UK)  ‘…’  “…” …' …''
English (US)   “…”  ‘…’ …''  …'
German  „…”  ‚…’ "…"'  \glqq…\grqq  \glq…\grq
French  «…»  «…» "‹…"›  \flqq…\frqq \flqq…\frqq
Danish  »…«  ›…‹  \frqq…\flqq  \frq…\flq
Lithuanian  „…” "…"'  \glqq…\grqq
Russian  «…»  „…” "‹…"›  "…"'  \flqq…\frqq  \glqq…\grqq
Ukrainian  «…»  \flqq…\frqq

Typesetting Quotation Marks

The dirtytalk package allows easier typesetting of quotations using the command \say{something}. The appropriate quotation marks are inserted by the macro and the package also allows for nested quotations. This makes entering quotations much easier.

How quotation glyphs are used can be loaded in the package options. As we can see from the above table, an English writer from the US might enter:

For example, we can use:

Which gives us:

"What did you mean when you said 'that' computer?"

For UK English, we load the options:

Which gives us:

'What did you mean when you said "that" computer?'

Being aware and manually tracking these typographic rules for quotations can make proofreading code difficult if we are just looking at LaTeX code. In that regard the dirtytalk package can help.

The French, Danish and Russian Languages

Entering quotation marks in English is easy but what if you working in another language, say French or Danish? Both languages make use of guillemets or « angle quotes ». The babel package uses the \flqq and the \frqq commands to typeset these glyphs but entering them can be a tad bit inconvenient.

To get our French quotations, we set the options as follows:

While there are single quillemets, ‹ and ›, that do exist in the babel package, they aren't typically used and just entering single angle brackets or the "greater than'' and "less than'' sign, < and > respectively, doesn't quite achieve the intended results.

In the Danish language, the quillemets are reversed while in the Russian language, there is a mix of guillemets and the German `low 9'' („) quotation marks.

In the Russian language, we can set our dirtytalk options to look like:

The dirtytalk package makes the typesetting of quotations in any language easier but there are some limitations. For one, it only allows for one set of nested quotations and anything beyond will take some user modification of the package. The second disadvantage to the package is that it can only handle quotations in one language. For documents that contain more than one language there are other options but for what it does, the dirtytalk package works nicely.

CTAN Quotation Packages in LaTeX

[latexpage]
In LaTeX, typing quotation marks in English (either US or UK) is easy but doing so consistently and across different languages is problematic. The problem stems from the array of different marks each language uses. We can see in table \ref{tab:table1} the different marks across languages. To ensure uniform typography, writers must be aware of and pay attention to various language rules, e.g. the primary and secondary quotation marks are swapped in UK and US English.

Table of primary and secondary quotation marks for different languages
Language Primary Secondary
English(UK) '…'
English(US) '…'
Danish »…« >…<
Lithuanian „…” No secondary quotes
French «…» «…»
German „…” ‚…’
Russian «…» „…”
Ukrainian «…» No Secondary Quotes

To make life easier, there are several packages on the CTAN database that makes the entering of quotation marks easier and efficient.

The dirtytalk Package

Manual

ftp://sunsite.icm.edu.pl/pub/CTAN/macros/latex/contrib/dirtytalk/dirtytalk.pdf

Description:

This is a very small LaTeX package that runs just 51 lines with just one command: \say. It can be loaded in the preamble: \usepackage{dirtytalk} and supports one nested quotation thus allowing for consistent primary and secondary application of quotation marks. Users can also redefine the quotation marks:

Here we can see the code for German quotations.

Pros

• Easy of use, only one command.
• Supports nested quotations.

Cons

• Only one nested quotation. No rule for adding more nested quotations without modifying package source code.
• No support for babel or polyglossia for quotes glyphes.

The csquotes Package

Manual

ftp://ftp.gust.org.pl/TeX/macros/latex/contrib/csquotes/csquotes.pdf

Description

A fairly large package with advanced facilities for inline and display quotations. Supports a wide-range of commands, environments and user-definable quotes. Quotes can be adjusted to the current languages. Autostyle can be turned by autostyle=true in the options. There are vast variety of commands to insert quotes: inline, quotes with sources, block-quotes with the support of changing language.

Pros

• Usage of babel and polyglossia packages for quotations marks.
• Custom number of nested quotes.
• Multilingual documents: options to set language for arbitrary quote.

Cons

• Vast number of options and commands --- too complex for most tasks.

The epigraph Package

Manual

ftp://ftp.tpnet.pl/pub/CTAN/macros/latex/contrib/epigraph/epigraph.pdf

Description

Another large package with vast of options to typeset the epigraphs and epigraphs lists. Provides commands for typesetting a single epigraph: \epigraph, and an epigraphs for typesetting a list of epigraphs. Can be typeset at either left, right and center of the typeblock.

Pros

• Easy typeset of quotes and quotes list.
• Many options for customization.
• Can be used with bibliography.

Cons

• Looks like it being frozen - long time without update.

The fancychapters Package

Manual

Only exists as LaTeX source code
ftp://sunsite.icm.edu.pl/pub/CTAN/macros/latex209/contrib/misc/fancychapters.sty

Description

The package adds two new commands:

• \Chapter{«quote»}{«title»}
• \Chapter{«quote»}[«abbrev»]{«title»}

Simple package to print fancy chapter headings.

Pros

• Automatic calculation of width to print fancy quotation and chapter name.

Cons

• Can be used only with book and report documentstyles.
• Works only for chapters: support for custom document parts like the empheq package.

The quotchap Package

Manual

ftp://ftp.gust.org.pl/TeX/macros/latex/contrib/quotchap/quotchap.pdf

Description

A very small package of only only 87 lines for typesetting quotations after the \chapter command by redefining both the \chapter macro and its starred version.

Pros

• Better than the fancychapter package as it is newer and more customizable.
• Color and fonts can be customized easily.

Cons

• Works only with book and report classes.

The epigram Package

Manual (in source code)

No official manual exists other than the LaTeX source code file.
ftp://ftp.tpnet.pl/pub/CTAN/macros/generic/misc/epigram.tex

Description

A package that provides an \epigram command to typeset epigrams that is both easy and simple to use. It is so small that the entire code can be shown here:

Pros

• So simple that it's hard to break.

Cons

• There are few (almost no) options for customization.
• Looks like nothing more than the simple example of package.

Conclusion

• As we can see, there are a number of interesting and useful CTAN packages for typesetting quotations, quotes and heading for chapters.If you need to just typeset simple quotes or quotations the best way is the dirtytalk package for it simplicity. This can be wrapped it in the flushleft environment.
• For simple chapter headings we can use the epigram or the fancychapters packages. The epigraph can also be used for typesetting list of quotes.
• The best package for typesetting quotes is the csquotes package as there is support for almost everything from automatic quotes symbols detection, inline-, block- quotes, vast options for customization, multilingual support and much more. This package is large, making it more suitable for large documents with many languages.