#+TITLE: LaTeX Configuration for Tikz export
:drawer:
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../css/htmlize.css"/>
#+HTML_HEAD: <link rel="stylesheet" type="text/css" href="../css/readtheorg.css"/>
#+HTML_HEAD: <script src="../js/jquery.min.js"></script>
#+HTML_HEAD: <script src="../js/bootstrap.min.js"></script>
#+HTML_HEAD: <script src="../js/jquery.stickytableheaders.min.js"></script>
#+HTML_HEAD: <script src="../js/readtheorg.js"></script>

#+PROPERTY: header-args:latex+ :tangle config.tex
#+PROPERTY: header-args:latex+ :exports code
:end:

* Packages
#+begin_src latex
\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
}
#+end_src

* Tikz related packages
#+begin_src latex
  \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
#+end_src

* Tikz Libraries
#+begin_src latex
  \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
#+end_src

* PGF Plot libraries and config
#+begin_src latex
  \usepgfplotslibrary{patchplots}
  \usepgfplotslibrary{groupplots}

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

* Setup size of figures
#+begin_src latex
  \newlength{\fheight}
  \newlength{\fwidth}

  \setlength{\fwidth}{85mm}
  \setlength{\fheight}{112mm}
#+end_src

* Setup Arrows style
#+begin_src latex
  \tikzset{>=Stealth}
  % Setup default Linewidth
  \tikzset{every path/.style={line width=1pt}}
#+end_src

* Colors
#+begin_src latex
  \usepackage{xcolor}% Color extension

  \definecolor{colorblack}{rgb}{0, 0, 0}
  \definecolor{colorblue}{rgb}{0, 0.4470, 0.7410}
  \definecolor{colorred}{rgb}{0.8500, 0.3250, 0.0980}
  \definecolor{coloryellow}{rgb}{0.9290, 0.6940, 0.1250}
  \definecolor{colorpurple}{rgb}{0.4940, 0.1840, 0.5560}
  \definecolor{colorgreen}{rgb}{0.4660, 0.6740, 0.1880}
  \definecolor{colorcyan}{rgb}{0.3010, 0.7450, 0.9330}
  \definecolor{colorbordeau}{rgb}{0.6350, 0.0780, 0.1840}

  % Main color
  \definecolor{maincolor}{RGB}{89, 9, 38}
  \definecolor{secondcolor}{RGB}{20, 9, 89}
#+end_src

* Control
** Blocks
#+begin_src latex
  \tikzset{%
    block/.style n args={2}{%
      draw,
      fill=white,
      minimum width  = #1,
      minimum height = #2,
    },
    block/.default={1.2cm}{1.0cm}
  }
#+end_src

** TODO Branches
#+begin_src latex
  \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]
#+end_src

** TODO Hand Writen Style
Usefull for schematic plots
#+begin_src latex
  \tikzstyle{handwriten}=[decorate,decoration={random steps,amplitude=0.1pt,segment length=0.8pt}]
#+end_src

** DAC
#+begin_src latex
  \tikzset{%
    DAC/.style={%
      draw,
      signal,
    }
  }
#+end_src

** ADC
#+begin_src latex
  \tikzset{%
    ADC/.style={%
      draw,
      signal,
      signal to = west,
    }
  }
#+end_src

** TODO Gain
Maybe use =isosceles= instead of regular polygon?
#+begin_src latex
  \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
    },
  }
#+end_src

** Add / Substract / Divide / Multiply block
#+begin_src latex
  \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={+}{}{}{}{},
  }
#+end_src

#+begin_src latex
  \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={+}{}{}{}{},
  }
#+end_src

* Plots
** Grid
#+begin_src latex
  \pgfplotsset{grid style={black}}
  \pgfplotsset{major grid style={black!30!white}}
  \pgfplotsset{minor grid style={black!10!white}}
  \pgfplotsset{xmajorgrids}
  \pgfplotsset{ymajorgrids}
#+end_src

** Lines
#+begin_src latex
  \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
#+end_src

** Ticks
#+begin_src latex
  \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}}
#+end_src

** Size
If =scale only axis=false= (the default), pgfplots will try to produce the desired width including labels, titles and ticks.
#+begin_src latex
  \pgfplotsset{scale only axis=true}
#+end_src

** Label
Used to align all of ylabel of one figure.
#+begin_src latex
  \pgfplotsset{ylabel absolute}
#+end_src

** Legend
#+begin_src latex
  % 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}
  }
#+end_src

** Upper and Lower bounds
#+begin_src latex
  \tikzstyle{upperbound}=[line cap=round, postaction={decorate,draw,decoration={border, segment length=0.2cm, amplitude=0.3cm, angle=60}}]
  \tikzstyle{lowerbound}=[line cap=round, postaction={decorate,draw,decoration={border, segment length=0.2cm, amplitude=0.3cm, angle=-60}}]
#+end_src

And we add the corresdonding
#+begin_src latex
  \pgfplotsset{
    /pgfplots/upperbound/.style 1 args={
      legend image code/.code={
        \draw[##1, upperbound]
          plot coordinates {
          (0cm,0cm)
          (0.6cm,0cm)
        }
      }
    }
  }
#+end_src

** Pole
#+begin_src latex
  \tikzset{%
    pole/.style{%
      color=red,
      cross out,
      draw,
      inner sep=0pt,
      outer sep=0pt,
      minimum size=#1pt
    },
    pole/.default={4}
  }
#+end_src

** Zero
#+begin_src latex
  \tikzset{%
    zero/.style{%
      color=red,
      circle,
      draw,
      inner sep=0pt,
      outer sep=0pt,
      minimum size=#1pt
    },
    zero/.default={4}
  }
#+end_src

* Mechanical
** Spring
#+begin_src latex
  \tikzset{%
    spring/.style={%
      thick,
      decoration={
        zigzag,
        pre length  = #1cm,
        post length = #1cm,
        segment length = 6
      },
      decorate
    },
    spring/.default={0.2}
  }
#+end_src

** Coil
#+begin_src latex
  \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}
  }
#+end_src

** Damper
#+begin_src latex
  \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}
  }
#+end_src

** Actuator
#+begin_src latex
  \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}
  }
#+end_src

** Ground
#+begin_src latex
  \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}
  }
#+end_src

** Force Sensor
#+begin_src latex
  \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}
  }
#+end_src

** Inertial Sensor
#+begin_src latex
  \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}
  }
#+end_src

** Axis Rotator
#+begin_src latex
  \newcommand{\AxisRotator}[1][rotate=0]{%
    \tikz [x=0.1cm,y=0.30cm,-stealth,#1] \draw (0,0) arc (-150:150:1 and 1);%
  }
#+end_src

** Cross
#+begin_src latex
  \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);
  }]

#+end_src

** Piezoelectric actuator
#+begin_src latex
  \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}
  }
#+end_src

** Voice coil
#+begin_src latex
  \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}
  }
#+end_src

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

* Misc
#+begin_src latex
  \tikzset{%
    labelc/.style= {%
      draw,
      fill=white,
      shape=circle,
      inner sep=2pt,
      outer sep=6pt,
    }
  }
#+end_src
