Search code examples
latexpdflatextikzmiktexpgf

Confusion on TikZ and PGF LaTeX packages - what they are, how they are stored by MiKTeX, and how they are interpreted by LaTeX compilers


I use VSCode to write LaTeX using the LaTeX-Workshop VSCode extension, MiKTeX, and pdflatex. The LaTeX-Workshop extension comes with the handy feature of being able to hover packages and view their documentation, but I noticed that this feature does not work on the tikz package. Screenshot with more detail.

I started looking into why this was the case, as the ctan page linked had this long and very detailed manual for TikZ and PGF. It turns out that MiKTeX does not even list a package called TikZ, which is confusing as my .tex files seem to compile fine when including the line \usepackage{tikz}, whereas attempting to use any other 'nonexistent' package would result in a compilation error.

I've gone through half a dozen webpages trying to understand what PGF and TikZ are in more detail, but it is very confusing. The ctan PGF page says "PGF is a macro package for creating graphics... that comes with a user-friendly syntax layer called TikZ", but I'm a little confused about what that means. Why would the package and syntax have different names?

My questions can be summed up as:

  • What exactly is PGF and TikZ? What's the difference between the two and is there a reason why they have different names?
  • How does my LaTeX compiler interpret "\usepackage{tikz}" if I don't have a package by that name installed?
  • What are TikZ libraries and what do lines like "\usetikzlibrary{arrows}" do exactly? Are they basically the same as packages?
  • If I'm using MiKTeX, where are the TikZ Libraries stored on my machine (Windows 10)? I have been able to find most other packages and their documentation within subfolders of "C:\Users\{USERNAME}\AppData\Local\Programs\MiKTeX 2.9\".
  • Instructions on how to link the TikZ/PGF manual pdf to MiKTeX so I can easily access the documentation in VSCode would be nice, but that is not really the focus of this post.

For clarification, I am not looking for a tutorial on how to use PGF/TikZ (there are many other good resources for that), nor am I looking for an overly high-level answer like "PGF/TikZ is a LaTeX package for creating graphics" that don't provide any more detail, I know that much already.


Solution

  • Pre-remark:

    There are two slightly different usages for the word "package" in the latex world, one is the traditional \usepackage{....} you know from your latex document, the other is the ctan/miktex/texlive package. Most of the time, a ctan/miktex/texlive package simply contains the latex package of the same name, but sometimes it can have a different name and/or contain multiple latex packages at once.


    • What exactly is PGF and TikZ? What's the difference between the two and is there a reason why they have different names?

    pgf provides the low level commands, like strokes etc. and tikz builds on top of this and uses the low level pgf commands to draw more complicate things, like geometrical shapes or rubber ducks (shameless plug)

    This division between the low level and high level code is very useful, because it allows the user to load just as much as necessary. Take for example the beamer class. It uses all kinds of low level pgf commands to draw decorations on the slides, so it loads (parts of) the pgf package. It does not need all the fancy stuff from the tikz package, so it does not load it, which safes tons of time, because loading all of tikz is relativity slow.

    • How does my LaTeX compiler interpret "\usepackage{tikz}" if I don't have a package by that name installed?

    You do have it installed, it is contained in the ctan/miktex/texlive pgf package. \usepackage{tikz} basically translates to \input{tikz.sty}. This file in return will load the latex pgf package (and many other things)

    • What are TikZ libraries and what do lines like "\usetikzlibrary{arrows}" do exactly? Are they basically the same as packages?

    yes, they are basically packages for tikz with which you can extend the capabilities of tikz even further.

    • If I'm using MiKTeX, where are the TikZ Libraries stored on my machine (Windows 10)? I have been able to find most other packages and their documentation within subfolders of "C:\Users\{USERNAME}\AppData\Local\Programs\MiKTeX 2.9\".

    You can look this up yourself in your .log file. Search it for tikz.sty and this and the following lines will tell you the location of all the files.

    • Instructions on how to link the TikZ/PGF manual pdf to MiKTeX so I can easily access the documentation in VSCode would be nice, but that is not really the focus of this post.

    If you open a new terminal in vscode (ctrl+shift+`), you can open the user guide by typing texdoc tikz or texdoc pgf (same file, just multiple ways to open it)