UP | HOME

LaTeX Configuration for Tikz Figures

Table of Contents

1 Packages

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage[french, english]{babel} % Last language is main language

\usepackage{lmodern} % Latin Modern Font
\usepackage{gensymb} % Generic symbols for both text and math mode

\usepackage{standalone} % Used to generate standalone Tikz

\usepackage{amsmath}   % Main math Package
\usepackage{mathtools} % Extension package to amsmath
\usepackage{amsthm}    % Typesetting theorems (AMS style)
\usepackage{amsfonts}  % More fonts from the AMS
\usepackage{textcomp}  % provide many text symbols
\usepackage{steinmetz} % For phase symbol

\usepackage{xstring}  % Utils to manipulate strings
\usepackage{etoolbox} % Add basic if/then
\usepackage{esvect}   % Beautyfull vectors
\usepackage{graphicx} % Enhanced support for graphics
\usepackage{grffile}  % Used by matlab2tikz

\usepackage{microtype} % typographic tuning
\usepackage{setspace}  % for line spacing, e.g. \onehalfspacing
\usepackage{tabularx}  % table features
\usepackage{enumitem}  % for simple list modifications
\usepackage{booktabs}  % better table support

\usepackage{stackengine} %

\usepackage[load-configurations=abbreviations]{siunitx} % SI units
\sisetup{
    locale = US,
    detect-all,
    range-phrase=--,
    range-units=single
}

2 Tikz related packages

\usepackage{tikz}       % Tikz
\usepackage{tikzscale}  % Used to scale Tikz graphics
\usepackage{adjustbox}  % Used to proper positioning of tikz pictures
\usepackage{circuitikz} % Draw electronic circuits
\usepackage{pgfpages}   % Needed to use notes
\usepackage{pgfplots}   % Used to plot functions

3 Tikz Libraries

\usetikzlibrary{arrows}                   % Arrow tip library
\usetikzlibrary{arrows.meta}              % Add some arrows
\usetikzlibrary{calc}                     % The library allows advanced Coordinate Calculations
\usetikzlibrary{intersections}            % calculate intersections of paths
\usetikzlibrary{matrix}                   %
\usetikzlibrary{patterns}                 %
\usetikzlibrary{shapes}                   % Defines circle and rectangle
\usetikzlibrary{shapes.geometric}         % Use for the shape diamond and isosceles triangle
\usetikzlibrary{snakes}                   % snake=coil and snake=zigzag using segment amplitude=10pt
\usetikzlibrary{positioning}              % Additional options for placing nodes
\usetikzlibrary{3d}                       % Plot 3D shapes
\usetikzlibrary{spy}                      % Creating a magnified area
\usetikzlibrary{decorations.text}         % Used to make text follows a curve
\usetikzlibrary{decorations.pathmorphing} % deformation of a path
\usetikzlibrary{decorations.markings}     % Used for spring and damper
\usetikzlibrary{babel}                    % A tiny library that make the interaction with the babel package easier
\usetikzlibrary{plotmarks}                % This library defines a number of plot marks
\usetikzlibrary{fit}                      % Used to make rectangle as nodes by specifying two points
\usetikzlibrary{backgrounds}              % Used to put things under others

4 PGF Plot libraries and config

\usepgfplotslibrary{patchplots}
\usepgfplotslibrary{groupplots}

\pgfplotsset{compat=newest}
\pgfplotsset{plot coordinates/math parser=false}

5 Setup size of figures

\newlength{\fheight}
\newlength{\fwidth}

\setlength{\fwidth}{85mm}
\setlength{\fheight}{112mm}

6 Setup Arrows style

\tikzset{>=Stealth}
% Setup default Linewidth
\tikzset{every path/.style={line width=1pt}}

7 Colors

\usepackage{xcolor}% Color extension

\definecolor{mycolor1}{RGB}{79,115,193}
\definecolor{mycolor2}{RGB}{213,91,53}
\definecolor{mycolor3}{RGB}{152,126,49}

8 Control

8.1 Blocks

\tikzset{%
  block/.style n args={2}{%
    draw,
    fill=white,
    minimum width  = #1,
    minimum height = #2,
  },
  block/.default={1.2cm}{1.0cm}
}

8.2 Branches

\tikzstyle{branch}=[fill,shape=circle,minimum size=4pt,inner sep=0pt]
\tikzstyle{->top}=[-{Stealth[color=black, scale=0.8]}, draw=white, double=black, double distance=1pt, line width=1pt]
\tikzstyle{<-top}=[{stealth[color=black, scale=0.8]}-, draw=white, double=black, double distance=1pt, line width=1pt]

8.3 Hand Writen Style

Usefull for schematic plots

\tikzstyle{handwriten}=[decorate,decoration={random steps,amplitude=0.1pt,segment length=0.8pt}]

8.4 DAC

\tikzset{%
  DAC/.style={%
    draw,
    signal,
  }
}

8.5 ADC

\tikzset{%
  ADC/.style={%
    draw,
    signal,
    signal to = west,
  }
}

8.6 Gain

\tikzset{%
  gain right/.style={%
    draw,
    regular polygon,
    regular polygon sides = 3,
    inner sep = 2pt,
    shape border rotate=-90
  },
  gain left/.style={%
    draw,
    regular polygon,
    regular polygon sides = 3,
    inner sep = 2pt,
    shape border rotate=90
  },
  gain top/.style={%
    draw,
    regular polygon,
    regular polygon sides = 3,
    inner sep = 2pt,
    shape border rotate=0
  },
  gain bottom/.style={%
    draw,
    regular polygon,
    regular polygon sides = 3,
    inner sep = 2pt,
    shape border rotate=180
  },
}

8.7 Add / Substract / Divide / Multiply block

\tikzset{% Add block with Circled operations
  addc/.style n args={5}{%
    draw,
    fill=white,
    circle,
    outer sep = 0pt,
    inner sep = 0pt,
    minimum size = 2em,
    execute at begin node={\LARGE $#1$},
    append after command={\pgfextra{\let\mainnode=\tikzlastnode}
      \ifx#2\empty\else
      node[draw, circle, outer sep=6pt, inner sep=0pt, above left] at (\mainnode.west) {$#2$}%
      \fi
      \ifx#3\empty\else
      node[draw, circle, outer sep=6pt, inner sep=0pt, above right] at (\mainnode.north) {$#3$}%
      \fi
      \ifx#4\empty\else
      node[draw, circle, outer sep=6pt, inner sep=0pt, below right] at (\mainnode.east) {$#4$}%
      \fi
      \ifx#5\empty\else
      node[draw, circle, outer sep=6pt, inner sep=0pt, below left] at (\mainnode.south) {$#5$}%
      \fi
      }
  },
  addc/.default={+}{}{}{}{},
}
\tikzset{% Add Block
  addb/.style n args={5}{%
    draw,
    fill=white,
    circle,
    outer sep = 0pt,
    inner sep = 0pt,
    minimum size = 2em,
    execute at begin node={\LARGE $#1$},
    append after command={\pgfextra{\let\mainnode=\tikzlastnode}
      \ifx#2\empty\else
      node[outer sep=2pt, inner sep=0pt, above left] at (\mainnode.west) {$#2$}%
      \fi
      \ifx#3\empty\else
      node[outer sep=2pt, inner sep=0pt, above right] at (\mainnode.north) {$#3$}%
      \fi
      \ifx#4\empty\else
      node[outer sep=2pt, inner sep=0pt, below right] at (\mainnode.east) {$#4$}%
      \fi
      \ifx#5\empty\else
      node[outer sep=2pt, inner sep=0pt, below left] at (\mainnode.south) {$#5$}%
      \fi
      }
  },
  addb/.default={+}{}{}{}{},
}

9 Plots

9.1 Default line caps

\pgfplotsset{
  every axis plot/.append style={line join=round},
  every axis plot/.append style={line cap=round},
}

9.2 Grid

\pgfplotsset{grid style={black}}
\pgfplotsset{major grid style={black!30!white}}
\pgfplotsset{minor grid style={black!10!white}}
\pgfplotsset{xmajorgrids}
\pgfplotsset{ymajorgrids}

9.3 Lines

\pgfplotsset{separate axis lines=false} % draw axis as rectangle and not as 4 lines
\pgfplotsset{every outer x axis line/.append style={black}}
\pgfplotsset{every outer y axis line/.append style={black}}
\pgfplotsset{axis background/.style={fill=white}}
\pgfplotsset{axis x line*=bottom} % solid line on the bottom with thin on the top
\pgfplotsset{axis y line*=left} % solid line on the left with thin on the right

9.4 Ticks

\pgfplotsset{every y tick label/.append style={font=\color{black}}}
\pgfplotsset{every y tick/.append style={black}}
\pgfplotsset{every x tick label/.append style={font=\color{black}}}
\pgfplotsset{every x tick/.append style={black}}

9.5 Size

If scale only axis=false (the default), pgfplots will try to produce the desired width including labels, titles and ticks.

\pgfplotsset{scale only axis=true}

9.6 Label

Used to align all of ylabel of one figure.

\pgfplotsset{ylabel absolute}

9.7 Legend

% https://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture#54834

% argument #1: any options
\newenvironment{customlegend}[1][]{%
  \begingroup
  % inits/clears the lists (which might be populated from previous
  % axes):
  \csname pgfplots@init@cleared@structures\endcsname
  \pgfplotsset{#1}%
}{%
  % draws the legend:
  \csname pgfplots@createlegend\endcsname
  \endgroup
}%

% makes \addlegendimage available (typically only available within an
% axis environment):
\def\addlegendimage{\csname pgfplots@addlegendimage\endcsname}

% definition to insert numbers
% \pgfkeys{/pgfplots/number in legend/.style={%
%     /pgfplots/legend image code/.code={%
%       \node at (0.125,-0.0225){#1}; % <= changed x value
%     },%
%   },
% }
\pgfplotsset{
  every legend to name picture/.style={west}
}

10 Mechanical

10.1 Spring

\tikzset{%
  spring/.style={%
    thick,
    decoration={
      zigzag,
      pre length  = #1cm,
      post length = #1cm,
      segment length = 6
    },
    decorate
  },
  spring/.default={0.2}
}

10.2 Coil

\tikzset{%
  coil/.style n args={2}{%
    thick,
    decoration={
      coil,
      pre length  = #1cm,
      post length = #2cm,
      segment length = 4
    },
    decorate
  },
  coil/.default={0.3}{0.3}
}

10.3 Damper

\tikzset{%
  damper/.style n args={2}{%
    thick,
    decoration={markings, mark connection node=dmp, mark=at position 0.5 with {
        \node (dmp) [thick,
                     inner sep = 0pt,
                     transform shape,
                     rotate  =-90,
                     minimum width  = #1pt,
                     minimum height = #2pt,
                     draw=none] {};
        \draw [thick] ($(dmp.north east)+(0.6*#2pt,0)$) -- (dmp.south east) -- (dmp.south west) -- ($(dmp.north west)+(0.6*#2pt,0)$);
        \draw [thick] ($(dmp.north)+(0,-0.3*#1pt)$) -- ($(dmp.north)+(0,0.3*#1pt)$);
      }
    },
    decorate
  },
  damper/.default={12}{3}
}

10.4 Actuator

\tikzset{%
  actuator/.style n args={2}{%
    thick,
    draw=none,
    decoration={
      markings,
      mark connection node=my node,
      mark=at position .5 with {
        \node [draw, inner sep=0pt, minimum width=#1cm, minimum height=#2cm,
        transform shape, fill=white] (my node) {};
      },
      mark=at position .0 with {
        \draw[<-] (0, 0) -- (my node);
      },
      mark=at position 1.0 with {
        \draw[<-] (0, 0) -- (my node);
      }
    },
    decorate
  },
  actuator/.default={0.5}{0.2}
}

10.5 Ground

\tikzset{%
  ground/.style n args={2}{%
    fill,
    pattern = north east lines,
    draw = none,
    anchor = north,
    minimum width  = #1cm,
    minimum height = #2cm,
    append after command={
      (\tikzlastnode.north west) edge (\tikzlastnode.north east)
    }
  },
  ground/.default={2.5}{0.3}
}

10.6 Force Sensor

\tikzset{%
  forcesensor/.style n args={2}{%
    rectangle,
    outer sep=0pt,
    inner sep=0pt,
    draw=black,
    fill=white!60!black,
    anchor=south,
    minimum width =#1cm,
    minimum height=#2cm,
    append after command={
      [every edge/.append style={
        thick,
        black,
      }]
      (\tikzlastnode.north west) edge (\tikzlastnode.south east)
      (\tikzlastnode.north east) edge (\tikzlastnode.south west)
    }
  },
  forcesensor/.default={2.0}{0.5}
}

10.7 Inertial Sensor

\tikzset{%
  inertialsensor/.style={%
    rectangle,
    outer sep=0pt,
    inner sep=0pt,
    draw=black,
    fill=white!60!black,
    anchor=south east,
    minimum size=#1cm,
    append after command={
      [every edge/.append style={
        thick,
        black,
      }]
      (\tikzlastnode.north west) edge (\tikzlastnode.south east)
      (\tikzlastnode.north east) edge (\tikzlastnode.south west)
    }
  },
  inertialsensor/.default={0.3}
}

10.8 Cross

\tikzstyle{cross}=[path picture={
  \draw[black]
  (path picture bounding box.south east) -- (path picture bounding box.north west) (path picture bounding box.south west) -- (path picture bounding box.north east);
}]

10.9 Piezoelectric actuator

\tikzset{%
  piezo/.style n args={3}{%
    draw,
    rectangle,
    minimum width  = #1cm,
    minimum height = #2cm,
    fill=blue!10!white,
    anchor=center,
    append after command={
      [every edge/.append style={
        thick,
        black,
      }]
      \foreach \i in {1,...,#3}{
        (${\i/(1+#3)}*(\tikzlastnode.north west)+{(1+#3-\i)/(1+#3)}*(\tikzlastnode.south west)+0.1*(#1,0)$) edge (${\i/(1+#3)}*(\tikzlastnode.north east)+{(1+#3-\i)/(1+#3)}*(\tikzlastnode.south east)-0.1*(#1,0)$)
      }
    }
  },
  piezo/.default={2}{4}{10}
}

10.10 Voice coil

\def\voicecoil#1#2#3{
  % ======================
  % Parameters
  % ======================
  \def\voicecoilw{#1} % Total Width
  \def\voicecoilh{#2} % Total Height

  \def\magnetw{\voicecoilw} % Width of the magnet
  \def\magneth{\voicecoilh/1.4} % Height of the magnet

  \def\magnetwb{0.15*\magnetw} % Width of the borders of the magnet
  \def\magnetmw{0.15*\magnetw} % Width of the middle part of the magnet
  \def\magnetwg{0.5*\magnetw} % Width of the gap of the magnet

  \def\magnethl{\magnetwb} % Height of the low part of the magnet
  \def\magnetmh{0.15*\magneth} % Height of the middle part of the magnet
  \def\magnethg{0.2*\magneth} % Height of the gap of the magnet
  % ======================

  \begin{scope}[shift={(0.5*\voicecoilw, 0.5*\voicecoilh)}, rotate=#3, shift={(0, -0.5*\voicecoilh)}]
    % ======================
    % Magnet
    % ======================
    \draw[fill=white] (0, 0) -| ++(0.5*\magnetw, \magneth) -| ++(-0.5*\magnetw+0.5*\magnetwg, -\magnethg) -| (0.5*\magnetw-\magnetwb, \magnethl) -| (-0.5*\magnetw+\magnetwb, \magneth-\magnethg) -| (-0.5*\magnetwg, \magneth) -| (-0.5*\magnetw, 0) -- (cycle);
    \begin{scope}[shift={(0, \magnethl)}]
      \draw[fill=red]  (-0.5*\magnetmw, 0) rectangle (0.5*\magnetmw, \magnetmh);
      \draw[fill=blue] (-0.5*\magnetmw, \magnetmh) rectangle (0.5*\magnetmw, 2*\magnetmh);
      % Top conductive Magnet
      \draw[fill=white] (-0.5*\magnetmw, 2*\magnetmh) -| (0.5*\magnetmw, -\magnethl+\magneth-\magnethg) -| ++(0.1, \magnethg) -| ++(-0.2-\magnetmw, -\magnethg) -| (-0.5*\magnetmw, \magnetmh);
    \end{scope}
    % ======================

    % ======================
    % Coil
    % ======================
    \pgfmathsetmacro{\coilwidth}{0.5*0.5*\magnetmw+0.5*0.1+0.25*\magnetwg}%
    \draw[] ( \coilwidth, 0.5*\magneth) -- ++(0, 0.7*\magneth);
    \draw[] (-\coilwidth, 0.5*\magneth) -- ++(0, 0.7*\magneth);
    % Point on the coil
    \foreach \x in {0,1,...,9}
    {
      \node[circle,inner sep=0.6pt,fill] at ( \coilwidth, \x*0.7*\magneth/10+0.5*\magneth);
      \node[circle,inner sep=0.6pt,fill] at (-\coilwidth, \x*0.7*\magneth/10+0.5*\magneth);
    }
    \draw[fill=white] (-0.5*\magnetw, 1.2*\magneth) rectangle ++(\magnetw, \magnethg);
    % ======================

    % ======================
    % Coordinates
    % ======================
    % Force
    \coordinate[] (vc_force) at (0, \magneth-0.5*\magnethg);
    % Coil
    \coordinate[] (vc_coil) at (0, \voicecoilh);
    % Magnet
    \coordinate[] (vc_magnet) at (0, 0);
    % Coil Wires
    \coordinate[] (vc_wire_one) at ( \coilwidth, 1.2*\magneth);
    \coordinate[] (vc_wire_two) at (-\coilwidth, 1.2*\magneth);
    % ======================
  \end{scope}
}

11 Optics

\tikzset{%
  ->-/.style={
    decoration={
      markings,
      mark = at position #1 with {\arrow{>}
      }
    },
    postaction={decorate}
  }
}
\tikzset{%
  -<-/.style={
    decoration={
      markings,
      mark = at position #1 with {\arrow{<}
      }
    },
    postaction={decorate}
  }
}

12 Misc

\tikzset{%
  labelc/.style= {%
    draw,
    fill=white,
    shape=circle,
    inner sep=2pt,
    outer sep=6pt,
  }
}

13 More Defaults specific to this paper

\tikzset{block/.default={0.8cm}{0.8cm}}
\tikzset{addb/.append style={scale=0.7}}
\tikzset{node distance=0.6}

Author: Thomas Dehaeze

Created: 2020-11-12 jeu. 10:39