Tikz Loop Over Coordinates

This is my belated response to a self-identified dupe on TeX.SE.

Background

The list in a \foreach loop is often long.

\foreach \pointA/\pointB in {{(1,0)}/{(2,2)},{(3,4)}/{(2,1)}} {
  \draw \pointA -- \pointB;
}

Imagine having several other loop variables. The code inside the {…} list won’t be readable.

Problem

How can I make it looks smarter?

\foreach \pointA/\pointB in {
  {(1,0)}/{(2,2)},{(3,4)}/{(2,1)}
} {
  \draw \pointA -- \pointB;
}

doesn’t work.

Inspiration

Recently, I was asked the way to make two graphs to illustrate a complex function.

[Read More]
LaTeX 

dvisvgm Guide for TikZ Pictures with Pattern Filling

A quick note of the right commands for the conversion

Problem

I had issues in getting a SVG from a TikZ picture containing a pattern filling: https://github.com/mgieseki/dvisvgm/issues/158. The reported issue was created last year, and it’s marked as fixed. Nonetheless, I had this issue even though my MikTeX was up-to-date.

Discussion

  1. I wanted to confirm whether my dvisvgm.exe, which had been shipped with MikTeX, was latest.
  2. I gave a MWE illustrating the problem with the graph of y = (x + 1)² with region under the curve filled with a pattern in the above issue. I knew that’s probably off-topic, but the package owner might have some insights about the problem. It turned out that PDF → SVG was partly supported by dvisvgm. Using DVI → SVG, I managed to get the filling pattern, but I lost the beautiful Computer Modern Roman fonts—that’s another issue.
  3. Hoping for a response from the owner for the previous problem, I started https://github.com/mgieseki/dvisvgm/issues/186. He suggested the use of --no-fonts or --font-format=woff. I observed no difference in the local filesystem. I uploaded everything I got through GitHub so that he could see what my dvisvgm.exe had output. I didn’t expected that the Computer Modern Roman fonts disappeared on GitHub side. If I hadn’t asked there, I would have never known that it’s the web browser’s security measure not to load the embedded font data in a SVG loaded from a server.

Skills learned

Meaning of some commonly used parameters of dvisvgm:

[Read More]
LaTeX  dvisvgm  SVG 

Distance From a Line

TikZ figures for a derivation from dot product

Background

A secondary school student posted a coordinate geometry question on a Discord homework help server. A helper replied to that question with an illustration of the Perpendicular Distance Formula

$$ d((x_1,y_1), L) = \frac{\lvert Ax_1 + By_1 + C \rvert}{\sqrt{A^2 + B^2}}, $$

where $L$ is the line $Ax + By + C = 0$ without proof.

Goal

To provide an illustrated derivation of this formula.

Recall

A basic property of the dot product: the “algebraic definition” is equivalent to the “geometric definition”.

[Read More]
LaTeX 

Elementary Log Inequality

TikZ figures for some graphical proofs of this inequality

Goal

Show that

$$ \ln (x) \le x - 1 $$

for all $x > 0$.

Proof by inverse functions

Think about their reflection along the line $y = x$ (i.e. their inverse function). Then we get

$$e^x \ge x + 1$$

for all $x \in \Bbb{R}$, which is obviously true.

Proof by definite integrals

Here’s a second proof using definite integrals.

PGF Plot for log inequality

Case 1: x > 1

The rectangle $[1,x] \times [0,1]$ contains the region under the graph of the reciprocal $y = 1/x$ in the domain $[1,x]$.

[Read More]
LaTeX 

A Plot for Euler's Constant

My first PGF Plot for step functions

I firsted tried with foreach, but that would create so much paths. I found them difficult to operate on later, for example, with PGF plots library fillbetween. const plot is a better solution.

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{patterns}
\usepgfplotslibrary{fillbetween}
\begin{document}
\newcommand\myN{8}
\pgfplotsset{
    axis lines=center,
    legend style={at={(1,1)},anchor=north east,fill=none},
    title style={at={(0.5,1.05)}},
    every axis x label/.style={
        at={(ticklabel* cs:1)},
        anchor=west,
    },
    every axis y label/.style={
        at={(ticklabel* cs:1)},
        anchor=south,
    },
}
\begin{tikzpicture}
\begin{axis}[
    title={sum of hatched region converges to Euler's constant},
    xlabel={$x$},
    ylabel={$y$},
    xmin=0,
    xmax={\the\numexpr\myN+2},
    ymin=0,
    ymax=1.3,
    xtick=\empty,
    ytick=\empty,
    extra x ticks={1, \myN},
    extra x tick labels={$1$, $n$},
    extra y ticks={1},
]
\addplot[name path=A,domain=1:\myN,samples=501,smooth] {1/x} \closedcycle;
\addlegendentry{$y = \frac{1}{x}$};
\addplot+[domain=1:\myN+1,samples=\myN+1,jump mark left,blue,mark=*,mark options={draw=blue,fill=blue}] {1/x};
\addlegendentry{$y = \frac{1}{\lfloor x \rfloor}$};
\addplot+[name path=B,domain=1:\myN+1,samples=\myN+1,jump mark left,mark=none, draw=none] {1/x} \closedcycle;
\addplot[pattern=north east lines] fill between [of=A and B];
\end{axis}
\end{tikzpicture}
\end{document}
Euler's constant

Euler's constant illustration

SVG generated by dvisvgm

[Read More]
LaTeX 

Simpler Diagram for Trigonometric Functions With Unit Circle

Background

In my previous post about unit circle and trigonometric functions, I included a graph with three trigonometry functions. I’m quite satisfied with my TikZ picture.

Problem

Unluckily, a secondary school student found that my diagram was too complicated.

Solution

\documentclass[tikz,border=2pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\usetikzlibrary{calc}
\newcommand\mytheta{110} %angle theta
\begin{document}
\begin{tikzpicture}[scale=2]
\coordinate[label=left:$O$] (O) at (0,0);
\coordinate[label=above left:${A = (\cos\theta, \sin\theta)}$] (A) at (\mytheta:1);
\coordinate[label=below left:${B = (\cos(-\theta), \sin(-\theta))}$] (B) at (-\mytheta:1);
\coordinate[label=right:$E$] (E) at (1,0);
\draw (O) circle (1);
\draw (A) -- (O)  node [midway, left] {$1$}
    -- (E);
\draw (B) -- (O);
\draw[-stealth] ($(O)!0.3!(E)$) arc (0:\mytheta:0.3) node[midway, above] {$\theta$};
\draw[-stealth] ($(O)!0.25!(E)$) arc (0:{-\mytheta}:0.25) node[midway, below] {$-\theta$};
\end{tikzpicture}
\end{document}

unit circle sine cosine

[Read More]
LaTeX 

Tikz Illustration for Triangle Inequality

I thought the code would be easy, but it turned out that I spent one hour and half to draw this figure.

triangle inequality

\documentclass[border=2pt,preview]{standalone}
\usepackage{tikz}
\usepackage{caption}
\newcommand{\sideA}{2}
\newcommand{\sideB}{3}
\newcommand{\sideC}{6}
\colorlet{myred}{red!40!yellow}
\colorlet{mygreen}{green!40}
\colorlet{myblue}{blue!20}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}[thick]
\coordinate (A) at (0,0);
\coordinate (B) at (\sideC,0);
\draw[<->,>=stealth,myred] (A) -- ++ (-\sideA, 0) node [pos=0.5,below] {$\color{myred}{a}$};
\draw[<->,>=stealth,mygreen] (B) -- ++ (\sideB, 0) node [pos=0.5,below] {$\color{mygreen}{b}$};
\draw[<->,>=stealth,myblue] (A) --  (B) node [pos = 0.5, below]{$\color{myblue}{c}$};
\draw[dashed,myred] (A) circle (\sideA);
\draw[dashed,mygreen] (B) circle (\sideB);
\end{tikzpicture}
\captionsetup{labelformat=empty}
\caption{triangle inequality ${\color{myred}{a}} + {\color{mygreen}{b}} >
  {\color{myblue}{c}}$ with $\color{myred}{a = \sideA}$,
  $\color{mygreen}{b = \sideB}$, $\color{myblue}{c = \sideC}$}
\end{figure}
\end{document}

The colors are customized for the Discord bot TeXit.

[Read More]
LaTeX 

LaTeX Table Background Color for TeXit

Problem

In my previous post about LaTeX tables, I used \cellcolor{white} to override the \rowcolor{cyan} in a cell in the leftmost column, which was the column type name.

On Discord, that cell became black because TeXit inverted black and white.

LaTeX table with TeXit problem

Discussion

I first tried blending black and white, and I found that black!27 and black!28 give a good approximation to Discord’s background color.

Thanks to this Discord Color Palette and a bot command channel on a server, I’ve observed that the desired color is RGB(54,57,62).

[Read More]
LaTeX  TeXit 

Line Equation in Intercept Form

LaTeX code for a TikZ figure

This is a first draft of a TikZ picture illustraing this classical formula to be used for math help channels.

Adding \caption{for the picture} without “Figure 1:” requires \usepackage{caption} and wrapping with \begin{figure}. It also possible to use the primitive TeX command \par, but it would be complicated to use that with standalone. In the previous post, the SVG picture from the LaTeX table in an article has too much useless whitespace around the table. I don’t bother to learn other packages, as I need time for other more important stuff.

[Read More]
LaTeX 

LaTeX Multirow Multicol Table Coloring

Inspiration

Number of two-digit numbers not containing the digit ‘8’.

Code

  • The standalone class can’t be used with \caption.
  • *{8}{c} means 8 c.
  • >{\columncolor{cyan}}c gives the column color. It \usepackage[table]{xcolor}.
  • \rowcolor{cyan} overrides the column colors.
  • \cellcolor{white} overrides the above two commands. It has to be carried out one-by-one without any fancy package.
  • \multicolumn{n}{<alignment>}{content} defines a multi-column cell.
  • \multirow{n}{*}{content} defines a multi-row cell. It \usepackage{multirow}. It can’t contain \multicolumn.
  • \cline{i-j} draws a horizontal line above the row from column i to column j.
  • No fancy way of coloring multirow cells without fancy packages.
  • \multirow{-9} at the last row creates a cell spanning nine rows from bottom to above, so that its content won’t be covered by colors of former rows.
  • \rotatebox[origin=[clr]]{angle}{content}: rotate content by angle anticlockwise with [clr] as the rotation center.
\documentclass[12pt]{article}
\usepackage{multirow}
\usepackage[table]{xcolor}
\usepackage{tikz}
\begin{document}
\begin{table}[htbp]
\centering
\caption{Sample \LaTeX{} multirow table}
\vspace*{1ex}
\label{multiprogram}
\begin{tabular}{|c|c|*{8}{c}>{\columncolor{cyan}}cc|}
\hline
\multicolumn{2}{|c|}{\multirow{2}{*}{}} & \multicolumn{10}{c|}{unit digit} \\\cline{3-12}
\multicolumn{2}{|c|}{} & 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & 9 \\\hline
& 1 & &  &  &  &  &  &  &  &  &  \\
& 2 & &  &  &  &  &  &  &  &  &  \\
& 3 & &  &  &  &  &  &  &  &  &  \\
& 4 & &  &  &  &  &  &  &  &  &  \\
& 5 & &  &  &  &  &  &  &  &  &  \\
& 6 & &  &  &  &  &  &  &  &  &  \\
& 7 & &  &  &  &  &  &  &  &  &  \\
\rowcolor{cyan} \cellcolor{white}
& 8 & &  &  &  &  &  &  &  &  &  \\
\multirow{-9}{*}{\rotatebox[origin=c]{90}{tens digit}}
& 9 & &  &  &  &  &  &  &  &  &  \\\hline
\end{tabular}
\end{table}
\end{document}

Result:

[Read More]
LaTeX  table