# LaTeX

## How to visualize I2C first byte structure in TikZ

In our previous post on How many bits does an I2C address have? we showed this diagram:

This diagram was made using TikZ using the following source code:

% I2C first byte diagram
% Author: Uli Koehler (https://techoverflow.net)
\documentclass[tikz, border=1mm]{standalone}
\usetikzlibrary{positioning, decorations.pathreplacing, arrows.meta}

% Source: https://tex.stackexchange.com/a/24133/45450
\makeatletter
\newcommand*{\textoverline}[1]{$\overline{\hbox{#1}}\m@th$}
\makeatother

\begin{document}
\begin{tikzpicture}
\node (A6) [draw, minimum height=7mm, minimum width=10mm] {$A_6$};
\node (A5) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A6] {$A_5$};
\node (A4) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A5] {$A_4$};
\node (A3) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A4] {$A_3$};
\node (A2) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A3] {$A_2$};
\node (A1) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A2] {$A_1$};
\node (A0) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A1] {$A_0$};
\node (RW) [draw, minimum height=7mm, minimum width=10mm, right=0cm of A0] {R/\textoverline{W}};

\coordinate[above left=0mm of A6] (AddrLeft);
\coordinate[above right=0mm of A0] (AddrRight);

\coordinate[below left=0mm of RW] (RWLeft);
\coordinate[below right=0mm of RW] (RWRight);

\draw[decorate,decoration={brace,amplitude=10pt}, minimum height=9mm]

\draw[decorate,decoration={brace,amplitude=8pt}, minimum height=6mm]
(RWRight) -- (RWLeft)

\end{tikzpicture}
\end{document}

and compiled to SVG using this Makefile – for details, see How to export TikZ graphics as SVG:

%.pdf: %.tex
pdflatex $< %.svg: %.pdf pdf2svg$< $@ all: I2CFirstByte.svg  Posted by Uli Köhler in LaTeX ## How to export TikZ graphics as SVG If you have a TikZ graphic, you can use the LaTeX standalone package to make the page fit the content: % Minimal TikZ standalone example \documentclass[tikz, border=1mm]{standalone} \begin{document} \begin{tikzpicture} \draw (0,0) node [] {My text}; \end{tikzpicture} \end{document} Assuming you have saved that file as MyDiagram.tex, you can convert it to a PDF and subsequently convert that PDF to a SVG using pdflatex MyDiagram.tex pdf2svg MyDiagram.pdf MyDiagram.svg which will generate this SVG: Note that the 1mm border around the content is intentional and recommended for most usecases. The background is transparent by default (but has been set to white in HTML on this blogpost to illustrate the extent of the SVG). You can also use this Makefile template: %.pdf: %.tex pdflatex$<

%.svg: %.pdf
pdf2svg $<$@

all: MyDiagram.svg

which allows you to automatically run the commands for one or multiple TeX files.

Posted by Uli Köhler in LaTeX

## Minimal TikZ standalone example

This example contains a simple TikZ graphic using the standalone package, i.e. it will be exported to a PDF/DVI where the page just fits the content:

\documentclass[tikz, border=1mm]{standalone}

\begin{document}
\begin{tikzpicture}
\draw (0,0) node [draw=black] {My text};
\end{tikzpicture}
\end{document}

The result looks like this:

Assuming the file is named MyDiagram.tex, you can compile it to PDF using

pdflatex MyDiagram.tex

Note that our example contains a 1mm border by default since this seems to be more suitable for the average usecase than having no border at all. In order to change that, you just need to modify the first line, e.g.

\documentclass[tikz]{standalone} % No border

or

\documentclass[tikz]{standalone, border=5mm} % 5mm border
Posted by Uli Köhler in LaTeX

## How to add box / border around node in TikZ

In TikZ, if you have a node like

\draw (0,0) node [] {My text};

you can add a border around it by using the draw=... attribute for the node:

\draw (0,0) node [draw] {My text};

You can also tell TikZ to draw it in blue:

\draw (0,0) node [draw=blue] {My text};

or tell it to draw it dashed:

\draw (0,0) node [draw, dashed] {My text};

You can also make the border thin or thick:

\draw (0,0) node [draw, very thin] {Very thin border};
\draw (0,-1) node [draw, thin] {Thin border};
\draw (0,-2) node [draw, semithick] {Semithick border};
\draw (0,-3) node [draw, thick] {Thick border};
\draw (0,-4) node [draw, very thick] {Very thick border};
\draw (0,-5) node [draw, ultra thick] {Ultra thick border};

Posted by Uli Köhler in LaTeX

## How to fix LaTeX missing footnotes in tabular environment

### Problem:

You have LaTeX code with a \footnote inside a \tabular like this:

\documentclass{scrartcl}

\begin{document}
\begin{tabular}{p{2cm}|p{2cm}|}
\textbf{Column A\footnote{This is a footnote!}} & \textbf{Column B}\\
1 & 2\\
3 & 4\\
\end{tabular}
\end{document}

but LaTeX/PDFLaTeX is not showing any of the footnotes you declared inside the \tabular environment.

### Solution:

Use this code just after your \documentclass declaration:

\usepackage{footnote}
\makesavenoteenv{tabular}

Then recompile your LaTeX code. The footnotes inside your tabular should now appear as expected.

Note that if you are using other environments than \begin{tabular} you might need to add more \makesavenoteenv declarations for the correct environments. The tabularx environment from the tabularx package works with footnotes out-of-the box without any additional packages!

Posted by Uli Köhler in LaTeX

## How to fix LaTeX \glqq or \grqq error ‘Undefined control sequence’

### Problem:

You have LaTeX code containing \glqq and/or \grqq like

\documentclass{scrartcl}

\begin{document}
\glqq Test\grqq
\end{document}

but when you try to compile it, you see an error message like this:

! Undefined control sequence.
l.5 \glqq
Test\grqq
?


### Solution:

In order to use \glqq or \grqq you need to include

\usepackage[ngerman]{babel}

after your \documentclass declaration.

In case you want to use another language, use the correct language specifier instead of ngerman.

Posted by Uli Köhler in LaTeX

## How to insert € symbol in LaTeX using UTF8

Note: This is the recommended method for using € and other symbols in LaTeX!

In order to insert the € symbol into your LaTeX document, first add

\usepackage[official]{eurosym}
\usepackage[utf8x]{inputenc}

to the top of your document, e.g. directly after your \documentclass line.

After that, you can use the normal € symbol anywhere in your document like this:

€

to insert an € symbol.

Posted by Uli Köhler in LaTeX

## How to insert € symbol in LaTeX using the eurosym package

Note: This method is no longer recommended and only provided for compatibility reasons! See How to insert Euro symbol in LaTeX using UTF8 for the recommended method instead!

In order to insert the € symbol into your LaTeX document, first add

\usepackage[official]{eurosym}

to the top of your document, e.g. directly after your \documentclass line.

After that, you can use

\euro{}

to insert an € symbol anywhere in your document.

Posted by Uli Köhler in LaTeX

## How to reduce page margins in LaTeX using the geometry package

To set or reduce the page margin (white space between the border of the page and the text) you can use the geometry package.

Set margin of all sides to 2cm:

\usepackage[left=20mm, right=20mm, top=20mm, bottom=20mm]{geometry}

To set just the left and right margin to 2cm, use

\usepackage[left=20mm, right=20mm]{geometry}

Posted by Uli Köhler in LaTeX

## How to convert a DVI file to SVG on the command line

If you want to convert my.dvi to my.svg, use this command

dvi2ps my.dvi | ps2eps - > my.eps && eps2svg my.eps

This produces my.svg – note that if my.svg already exists, eps2svg will create my_1.svg, my_2.svg and so on and will not overwrite my.svg!

You can also use this shell function:

function dviToSVG { dvi2ps "$1" | ps2eps - > "${1%.*}.eps" && eps2svg "${1%.*}.eps" "${1%.*}.svg" ; }

Usage example:

dviToSVG my.dvi # Produces my.svg

Posted by Uli Köhler in LaTeX, Linux, Shell

## Minimal LaTeX booktabs example

This is a minimal example using the LaTeX booktabs package which you can use & modify to create your own table.

\documentclass{scrartcl}
\usepackage{booktabs}
\usepackage[utf8x]{inputenc}

\begin{document}

\begin{table}[h!]
\begin{center}
\caption{Minimal booktabs example.}
\label{tab:table1}
\begin{tabular}{l|c|r}
\toprule % <-- Toprule here
\textbf{Column 1} & \textbf{Column 2} & \textbf{Column 3}\\
$\alpha$ & $\beta$ & $\gamma$ \\
\midrule % <-- Midrule here
A & 10.23 & a\\
B & 45.678 & b\\
C & 99.987 & c\\
\bottomrule % <-- Bottomrule here
\end{tabular}
\end{center}
\end{table}
\end{document}

Render it using

pdflatex booktabs.tex

The table looks like this:

Posted by Uli Köhler in LaTeX

## How to fix LaTeX error File siunitx.sty’ not found on Ubuntu

### Problem:

You want to compile your LaTeX file on Ubuntu using latex or pdflatex but you see an error message like this:

! LaTeX Error: File siunitx.sty' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: sty)

### Solution

sudo apt -y install texlive-science

This will install, among many other LaTeX files, siunitx.sty.

Posted by Uli Köhler in LaTeX

## How to fix LaTeX error File environ.sty’ not found on Ubuntu

### Problem:

You want to compile your LaTeX file on Ubuntu using latex or pdflatex but you see an error message like this:

! LaTeX Error: File environ.sty' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: sty)

### Solution

sudo apt -y install texlive-latex-extra

This will install, among many other LaTeX files, environ.sty.

Posted by Uli Köhler in LaTeX, Linux

## How to fix LaTeX error Package babel Error: Unknown option ngerman’ on Ubuntu

### Problem:

You want to compile your LaTeX file on Ubuntu using latex or pdflatex but you see an error message like this:

! Package babel Error: Unknown option ngerman'. Either you misspelled it
(babel)                or the language definition file ngerman.ldf was not foun
d.

See the babel package documentation for explanation.
Type  H <return>  for immediate help


### Solution

sudo apt -y install texlive-lang-german

This will install, among many other LaTeX files, the files for the ngerman language.

Posted by Uli Köhler in LaTeX, Linux

## How to fix Ubuntu LaTeX ! I can’t find file ecrm1200′.

### Problem:

You want to compile your LaTeX file on Ubuntu using latex or pdflatex but you see an error message like this:

kpathsea: Running mktexmf ecrm1200
! I can't find file ecrm1200'.
<*> ...ljfour; mag:=1; nonstopmode; input ecrm1200

Please type another input file name
! Emergency stop.
<*> ...ljfour; mag:=1; nonstopmode; input ecrm1200

Transcript written on mfput.log.
grep: ecrm1200.log: No such file or directory
mktextfm: mf-nowin -progname=mf \mode:=ljfour; mag:=1; nonstopmode; input ecrm1200' failed to make ecrm1200.tfm.
kpathsea: Appending font creation commands to missfont.log.

! Font T1/cmr/m/n/12=ecrm1200 at 12.0pt not loadable: Metric (TFM) file not fou
nd.
<to be read again>
relax
l.105 \fontencoding\encodingdefault\selectfont


### Solution

sudo apt -y install texlive-fonts-recommended

This will install, among many other LaTeX files, ecrm1200.mf and ecrm1200.tfm.

.

Posted by Uli Köhler in LaTeX, Linux

## How to fix LaTeX Error: File scrartcl.cls’ not found on Ubuntu

### Problem:

You want to compile your LaTeX file on Ubuntu using latex or pdflatex but you see an error message like this:

! LaTeX Error: File scrartcl.cls' not found.

Type X to quit or <RETURN> to proceed,
or enter new name. (Default extension: cls)


### Solution

sudo apt -y install texlive-latex-recommended

This will install, among many other LaTeX files, scrartcl.cls.

Posted by Uli Köhler in LaTeX, Linux

## How to fix KaTeX parse error: KaTeX doesn’t work in quirks mode

### Problem:

You’re using KaTeX on a website, but every time you try to render, you encounter the following error message:

Uncaught Error: KaTeX parse error: KaTeX doesn't work in quirks mode.
at new e (VM697 katex.min.js:1)
at Object.l [as render] (VM697 katex.min.js:1)
[...]

### Solution:

KaTeX requires a valid DOCTYPE declaration on your HTML page, so add this line at the top of your HTML file(s), above <html>

<!DOCTYPE html>

Thanks to @xymostech on GitHub for the original solution in the the KaTeX issue tracker:

Posted by Uli Köhler in Javascript, LaTeX

## Problem:

You want to compile a LaTeX file containing CircuiTikZ code but you get the following error:

LaTeX Error: Unknown option fetbodydiode' for package circuitikz'

## Solution:

You have an outdated CircuiTikZ version (fetbodydiode is in TeXLive 2016+).  Depending on your distribution, there are several ways to update CircuiTikZ:

For ubuntu, see this post to update to TeXLive 2016

For other distributions, see this post using tlmgr

Posted by Uli Köhler in LaTeX

## Problem:

You want to compile a LaTeX file containing CircuiTikZ code but you get the following error:

Error: I do not know the key '/tikz/elmech'

## Solution:

First, be sure that the circuitikz package is included, i.e. there’s a line like

\usepackage{circuitikz}

in your LaTeX file.

If that is the case, you likely have an outdated CircuiTikZ version (elmech is in TeXLive 2016+).  Depending on your distribution, there are several ways to update CircuiTikZ:

For ubuntu, see this post to update to TeXLive 2016

For other distributions, see this post using tlmgr

Posted by Uli Köhler in LaTeX

## Problem:

You’re using latex or pdflatex to compile a .tex file, but you get an error message similar to this one (the solution will work for any missing file, not just utf8x.def):

! LaTeX Error: File utf8x.def' not found.


Now you’re wondering which package you need to install

## Solution 1: Install everything

This problem can often be fixed once and for all by just installing all packages:

sudo apt-get install texlive-full

However, this pulls in a huge amount of packages and is therefore not recommended for most situations.

## Solution 2: Install only required package

You can use apt-file to find the package containing the missing file and install it.

First, update the list of files in all known packages (sudo apt-get install apt-file if required):

sudo apt-file update

You only need to do this once every few months or so, before you use apt-file.

Then, look for the missing file (replace utf8x.def by your missing file):

\$ apt-file search utf8x.def
texlive-lang-japanese: /usr/share/texlive/texmf-dist/tex/latex/bxbase/bxutf8x.def
texlive-latex-extra: /usr/share/texlive/texmf-dist/tex/latex/ucs/utf8x.def
texlive-luatex: /usr/share/texlive/texmf-dist/tex/lualatex/luainputenc/lutf8x.def


Now it takes some educated guessing which of the three listed packages (texlive-lang-japanese, texlive-latex-extra, texlive-luatex) needs to be installed. In this case, texlive-latex-extrais the correct choice as the other packages list the missing file only in some subdirectory of package (like luainputenc). If in doubt, you can just install all of the listed packages.

Posted by Uli Köhler in LaTeX, Linux