\tikzset{
  % bands
  sc band con/.style={   draw=fg0, thick},
  sc band val/.style={   draw=fg0, thick},
  sc band vac/.style={   draw=fg1, thick},
  sc band/.style={       draw=fg0, thick},
  sc fermi level/.style={draw=fg-aqua,dashed,thick},
  % electron filled
  sc occupied/.style={
	pattern=north east lines,
	pattern color=fg-aqua,
	draw=none
  },
  % materials
  sc p type/.style={     draw=none,fill=bg-yellow!20},
  sc n type/.style={     draw=none,fill=bg-blue!20},
  sc metal/.style={      draw=none,fill=bg-purple!20},
  sc oxide/.style={      draw=none,fill=bg-green!20},
  sc separate/.style={   draw=fg0,dotted},
  % crystal
  miller dir/.style={->,color=fg-purple,draw=fg-purple, thick},
  miller plane/.style={fill=bg-purple,fill opacity=0.6,draw=fg-purple,color=fg-purple},
}

\newcommand\drawDArrow[4]{
  \draw[<->] (#1,#2) -- (#1,#3) node[midway,right] () {#4};
}
% Band bending down at L-R interface: BendH must be negative
% need two functions for different out= angles, or use if else on the sign of BendH
\newcommand\leftBandAuto[2]{
  \directlua{
    if \tkLBendH == 0 then
    tex.print([[(\tkLx,#2) \ifblank{#1}{}{node[anchor=east] \detokenize{{#1}}} -- (\tkLW,#2) ]])
    else
    if \tkLBendH > 0 then
    angle = 180+45
    else
    angle = 180-45
    end
    tex.sprint([[(\tkLx,#2) \ifblank{#1}{}{node[anchor=east] \detokenize{{#1}}}
    -- (\tkLW-\tkLBendW,#2) to[out=0,in=]], angle, [[](\tkLW,#2+\tkLBendH)]])
    end
  }
  % % \ifthenelse{\equal{\tkLBendH}{0}}%
  %   % {%
  %     \ifthenelse{\tkLBendH > 0}%
  %     {\pgfmathsetmacro{\angle}{-45}}%
  %     {\pgfmathsetmacro{\angle}{45}}%
  %   % }
}
\newcommand\rightBandAuto[2]{
  \directlua{
  if \tkRBendH == 0 then
	%-- tex.print([[\rightBand{#1}{#2}]])
  tex.print([[(\tkRx,#2) -- (\tkW,#2)]]) %-- \ifblank{#1}{}{node[anchor=west] \{#1\}}]])
  else
	if \tkRBendH > 0 then
	angle = -45
	else
	angle = 45
	end
	tex.sprint([[(\tkRx,#2+\tkRBendH) to[out=]], angle, [[,in=180] (\tkRx+\tkRBendW,#2) -- (\tkW,#2) ]])
	%-- \ifblank{#1}{}{node[anchor=west] \{#1\}} ]])
  end
  if "\luaescapestring{#1}" \string~= "" then
	tex.print([[node[anchor=west] \detokenize{{#1}} ]])
  end
  }
  % \ifthenelse{\equal{\tkRBendH}{0}}%
  %   {\rightBand{#1}{#2}}
  %   {%
  %     \ifthenelse{\tkRBendH > 0}%
  %     {\pgfmathsetmacro{\angle}{-45}}%
  %     {\pgfmathsetmacro{\angle}{45}}%
  %     (\tkRx,#2+\tkRBendH) to[out=45,in=180] (\tkRx+\tkRBendW,#2) -- (\tkW,#2) 
  %     \ifblank{#1}{}{node[anchor=west]{#1}} 
  %   }
}
\newcommand\leftBandDown[2]{
  (\tkRx,#2+\tkRBendH) to[out=45,in=180] (\tkRx+\tkRBendW,#2) -- (\tkW,#2) 
  \ifblank{#1}{}{node[anchor=west]{#1}} 
}
\newcommand\rightBandDown[2]{
  (\tkRx,#2+\tkRBendH) to[out=45,in=180] (\tkRx+\tkRBendW,#2) -- (\tkW,#2) 
  \ifblank{#1}{}{node[anchor=west]{#1}} 
}
% Band bending down at L-R interface: BendH must be positive
\newcommand\leftBandUp[2]{
  (\tkLx,#2) \ifblank{#1}{}{node[anchor=east]{#1}} 
  -- (\tkLW-\tkLBendW,#2) to[out=0,in=180+45] (\tkLW,#2+\tkLBendH) 
}
\newcommand\rightBandUp[2]{
  (\tkRx,#2+\tkRBendH) to[out=-45,in=180] (\tkRx+\tkRBendW,#2) -- (\tkW,#2) 
  \ifblank{#1}{}{node[anchor=west]{#1}} 
}
% Straight band
\newcommand\leftBand[2]{
  (\tkLx,#2) \ifblank{#1}{}{node[anchor=east]{#1}} -- (\tkLW,#2) 
}
\newcommand\rightBand[2]{
  (\tkRx,#2) -- (\tkW,#2) \ifblank{#1}{}{node[anchor=west]{#1}} 
}

\newcommand\drawAxes{
  \draw[->] (0,0) -- (\tkW+0.2,0) node[anchor=north] {$x$};
  \draw[->] (0,0) -- (0,\tkH+0.2) node[anchor=east] {$E$};
}

\newcommand\tkXTick[2]{
  \pgfmathsetmacro{\tickwidth}{0.1}
  \draw (#1, -\tickwidth/2) -- (#1, \tickwidth/2) node[anchor=north] {#2};
}
\newcommand\tkYTick[2]{
  \pgfmathsetmacro{\tickwidth}{0.1}
  \draw (-\tickwidth/2, #1) -- (\tickwidth/2,#1) node[anchor=east] {#2};
}

% 3D HEXAGON
% 1: name
% 2: center
% 3-5: a1-3
\newcommand{\setHexagonPoints}[5]{
  \coordinate (#1-M) at #2;
  \coordinate (#1-A) at ($#2+(xyz cylindrical cs:radius=#3,angle=0)$);
  \coordinate (#1-C) at ($#2+(xyz cylindrical cs:radius=#4,angle=120)$);
  \coordinate (#1-E) at ($#2+(xyz cylindrical cs:radius=#5,angle=240)$);
  \coordinate (#1-D) at ($#2+(xyz cylindrical cs:radius=#3,angle=180)$);
  \coordinate (#1-F) at ($#2+(xyz cylindrical cs:radius=#4,angle=300)$);
  \coordinate (#1-B) at ($#2+(xyz cylindrical cs:radius=#5,angle=60)$);
}
\newcommand\drawHexagon[1]{ \draw (#1-A) -- (#1-B) -- (#1-C) -- (#1-D) -- (#1-E) -- (#1-F) -- (#1-A); }
\newcommand\drawHexagonBack[1]{ \draw (#1-A) -- (#1-B) -- (#1-C) -- (#1-D); }
\newcommand\drawHexagonFront[1]{ \draw (#1-D)-- (#1-E) -- (#1-F) -- (#1-A); }
% 1: r 
% 2: z
\newcommand\drawHexagonCS[2]{
  \draw[->] (0,0,0) -- ++(xyz cylindrical cs:radius=1.5*#1,angle=000) node[anchor=west]{$a_1$};
  \draw[->] (0,0,0) -- ++(xyz cylindrical cs:radius=1.5*#1,angle=120) node[anchor=south east]{$a_2$};
  \draw[->] (0,0,0) -- ++(xyz cylindrical cs:radius=1.5*#1,angle=240) node[anchor=north east]{$a_3$};
  \draw[->] (0,0,0) -- ++(0,0,1.5*#2) node[anchor=south]{$a_4$};
}
% vertically connect two hexagons
\newcommand\drawHexagonConnectionsBack[2]{
  \draw (#1-A) -- (#2-A);
  \draw (#1-B) -- (#2-B);
  \draw (#1-C) -- (#2-C);
  \draw (#1-D) -- (#2-D);
}
\newcommand\drawHexagonConnectionsFront[2]{
  \draw (#1-E) -- (#2-E);
  \draw (#1-F) -- (#2-F);
}
% 3D RECTANGLES
% 1: name
% 2: center
% 3,4: w,l
\newcommand{\setRectPoints}[4]{
  \coordinate (#1-M) at #2;
  \coordinate (#1-A) at ($#2+(-#3*0.5, #4*0.5,0)$);
  \coordinate (#1-B) at ($#2+(-#3*0.5,-#4*0.5,0)$);
  \coordinate (#1-C) at ($#2+( #3*0.5,-#4*0.5,0)$);
  \coordinate (#1-D) at ($#2+( #3*0.5, #4*0.5,0)$);
}
\newcommand\drawRect[1]{ \draw (#1-A) -- (#1-B) -- (#1-C) -- (#1-D) -- cycle; }
\newcommand\drawRectBack[1]{ \draw (#1-D) -- (#1-A) -- (#1-B) -- (#1-C); }
\newcommand\drawRectFront[1]{ \draw (#1-C)-- (#1-D); }
% Coordinate System
% 1: x,y 
% 2: z 
\newcommand\drawRectCS[2]{
  \draw[->] (0,0,0) -- ++(#1, 0, 0) node[anchor=west]{$x$};
  \draw[->] (0,0,0) -- ++( 0,#1, 0) node[anchor=south east]{$y$};
  \draw[->] (0,0,0) -- ++( 0, 0,#2) node[anchor=south]{$z$};
}
% vertically connect two rects
\newcommand\drawRectConnectionsBack[2]{
  \draw (#1-A) -- (#2-A);
  \draw (#1-B) -- (#2-B);
}
\newcommand\drawRectConnectionsFront[2]{
  \draw (#1-C) -- (#2-C);
  \draw (#1-D) -- (#2-D);
}
