DEV Community

KatagiriSo
KatagiriSo

Posted on

2

Tikz example

Suppose you want to draw a diagram like this.

picture

package to be used

\usepackage{tikz}
\usetikzlibrary{intersections, calc, arrows}
Enter fullscreen mode Exit fullscreen mode

I drew the code for two blacked-out triangles side by side.
CALC was used for the calculations.

command definition

I created a command to draw a triangle. I define the coordinates, define the relationship between the coordinates, and then paint it with a fill.

\newcommand{\myTriangle}[1]{
\coordinate (myA) at ($#1+(0,0)$);
\coordinate (myB) at ($#1+(1,1)$);
\coordinate (myC) at ($#1+(2,0)$);
\fill (myA)--(myB)--(myC)--cycle;}
Enter fullscreen mode Exit fullscreen mode

Tikz body

I draw various instructions in the begin and end of the tikzpicture.

\begin{tikzpicture}
...
\end{tikzpicture}
Enter fullscreen mode Exit fullscreen mode

Defining vectors

Define a vector and define a triangle as a place by adding the vectors

\coordinate (R) at (2,0);
\coordinate (U) at (0,1);
\coordinate (LD) at (-1,-1);
\coordinate (RD) at (1,-1);

\coordinate (A) at (0,0);
\coordinate (B) at ($(A)+3*(R)$);
\coordinate (C) at ($(A)+3*0.5*(R)+4*(U)$);
Enter fullscreen mode Exit fullscreen mode

Drawing a triangle

command to draw a triangle.

\myTriangle{(A)}
\myTriangle{(B)}
\myTriangle{(C)}
Enter fullscreen mode Exit fullscreen mode

Drawing legs.

Specify the coordinates of the legs by adding and subtracting vectors, and draw a line.

\draw ($(A)+0.5*(R)+1*(U)$) -- ($(C)$);
\draw ($(B)+0.5*(R)+1*(U)$) -- ($(C)+(R)$);
\draw ($(A)$) -- ($(A)-0.5*(R)-4*(U)$);
\draw ($(A)+(R)$) -- ($(A)+(R)+0.5*(R)-4*(U)$);
\draw ($(B)$) -- ($(B)-0.5*(R)-4*(U)$);
\draw ($(B)+(R)$) -- ($(B)+(R)+0.5*(R)-4*(U)$);
Enter fullscreen mode Exit fullscreen mode

If you define it well, you will be able to do complex drawings.

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay