%%
%% kleinbottle.sty
%%
%% Copyright (C) 2026 Benjamim Lima Neto
%%
%% This work may be distributed and/or modified under
%% the conditions of the LaTeX Project Public License,
%% either version 1.3c of this license or (at your option)
%% any later version.
%%
%% The latest version of this license is at:
%% https://www.latex-project.org/lppl.txt
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is Benjamim Lima Neto.
%%
%% Contact information is provided in README.md.
%%
%% This work consists of kleinbottle.sty, kleinbottle.tex,
%% README.md, and the derived file kleinbottle.pdf.
%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{kleinbottle}
  [2026/11/30 v1.01 Klein bottle miniature]

% --------------------------------------------------
% Required packages
% --------------------------------------------------

\RequirePackage{xcolor}
\RequirePackage{tikz}
\RequirePackage{graphicx}

% --------------------------------------------------
% User options and default values
% --------------------------------------------------

\pgfkeys{
  /kleinbottle/.is family,
  /kleinbottle,
  color/.store in=\kleinbottle@color,
  opacity/.store in=\kleinbottle@opacity,
  scale/.store in=\kleinbottle@scale,
  rotate/.store in=\kleinbottle@rotate,
  left/.store in=\kleinbottle@left,
  right/.store in=\kleinbottle@right,
  up/.store in=\kleinbottle@up,
  down/.store in=\kleinbottle@down,
  defaults/.style={
    color=black,
    opacity=100,
    scale=1,
    rotate=0,
    left=0,
    right=0,
    up=0,
    down=0
  }
}

% --------------------------------------------------
% Main command
% --------------------------------------------------

\DeclareRobustCommand{\klein}[1][]{%
  \leavevmode
  \hbox\bgroup
  \begingroup
  %
  % Reset defaults and process user options
  %
  \pgfkeys{/kleinbottle/.cd, defaults, #1}%
  %
  % Validate opacity
  %
  \pgfmathparse{
    (\kleinbottle@opacity >= 0) *
    (\kleinbottle@opacity <= 100)
  }%
  \ifdim\pgfmathresult pt=0pt\relax
    \PackageError{kleinbottle}
      {Opacity must be between 0 and 100}
      {Use opacity=100 for full opacity.}%
    \def\kleinbottle@opacity{100}%
  \fi
  %
  % Validate scale
  %
  \pgfmathparse{\kleinbottle@scale > 0}%
  \ifdim\pgfmathresult pt=0pt\relax
    \PackageError{kleinbottle}
      {Scale must be positive}
      {Use scale=1 for the original size.}%
    \def\kleinbottle@scale{1}%
  \fi
  %
  % Validate rotation
  %
  \pgfmathparse{
    (\kleinbottle@rotate >= 0) *
    (\kleinbottle@rotate <= 360)
  }%
  \ifdim\pgfmathresult pt=0pt\relax
    \PackageError{kleinbottle}
      {Rotation must be between 0 and 360}
      {Use rotate=0 for the original orientation.}%
    \def\kleinbottle@rotate{0}%
  \fi
  %
  % Convert percentage to TikZ opacity
  %
  \pgfmathsetmacro{\kleinbottle@alpha}
    {\kleinbottle@opacity/100}%
  %
  % Compute proportional scaling factor
  %
  \pgfmathsetmacro{\kleinbottle@factor}
    {\kleinbottle@scale}%
  %
  % Compute horizontal and vertical displacements.
  % All displacement values are expressed in centimeters
  % and are independent of scaling and rotation.
  %
  \pgfmathsetlengthmacro{\kleinbottle@dx}
    {((\kleinbottle@right)-(\kleinbottle@left))*1cm}%
  %
  \pgfmathsetlengthmacro{\kleinbottle@dy}
    {((\kleinbottle@up)-(\kleinbottle@down))*1cm}%
  %
  % Apply horizontal displacement
  %
  \kern\kleinbottle@dx\relax
  %
  % Apply vertical displacement and proportional scaling
  %
  \raisebox{\kleinbottle@dy}{%
    \scalebox{\kleinbottle@factor}{%
      %
      % ----------------------------------------------
      % Klein bottle illustration
      % ----------------------------------------------
      %
      \begin{tikzpicture}[
        baseline=-0.1ex,
        scale=0.14,
        rotate=\kleinbottle@rotate
      ]
      %
      % Apply opacity to the complete illustration
      % rather than to individual drawing paths.
      %
      \begin{scope}[
        transparency group,
        opacity=\kleinbottle@alpha
      ]
        \begin{scope}[
          color=\kleinbottle@color,
          opacity=1,
          line width=0.55pt,
          line cap=round,
          line join=round
        ]
        %
        % 1. Left outer wall
        %
        \draw (-0.75, 0.3)
          .. controls (-0.75, 1.0) and (-0.28, 1.35)
          .. (-0.28, 1.9)
          .. controls (-0.28, 2.55) and (0.95, 2.55)
          .. (0.95, 1.8)
          .. controls (0.95, 1.45) and (0.72, 1.25)
          .. (0.50, 1.05);
        %
        % 2. Inner neck wall
        %
        \draw (0.12, 1.55)
          .. controls (0.10, 1.70) and (0.10, 1.80)
          .. (0.10, 1.9)
          .. controls (0.10, 2.15) and (0.55, 2.15)
          .. (0.55, 1.8)
          .. controls (0.55, 1.60) and (0.45, 1.42)
          .. (0.32, 1.25);
        %
        % 3. Right outer wall
        %
        \draw (0.75, 0.3)
          .. controls (0.75, 0.65) and (0.65, 0.90)
          .. (0.50, 1.05);
        %
        \draw (0.32, 1.25)
          .. controls (0.20, 1.38) and (0.14, 1.48)
          .. (0.12, 1.55);
        %
        % 4. Internal tube
        %
        \draw[
          densely dashed,
          dash pattern=on 1.2pt off 1pt
        ]
          (0.50, 1.05)
          .. controls (0.28, 0.85) and (0.18, 0.35)
          .. (0.18, -0.35);
        %
        \draw[
          densely dashed,
          dash pattern=on 1.2pt off 1pt
        ]
          (0.32, 1.25)
          .. controls (0.02, 0.98) and (-0.18, 0.35)
          .. (-0.18, -0.35);
        %
        % 5. Bottom base
        %
        \draw (-0.75, 0.3)
          .. controls (-0.75, -0.25) and (-0.35, -0.35)
          .. (-0.18, -0.35);
        %
        \draw (0.75, 0.3)
          .. controls (0.75, -0.25) and (0.35, -0.35)
          .. (0.18, -0.35);
        %
        % 6. Bottom opening
        %
        \draw (0, -0.35) ellipse (0.18 and 0.06);
        %
        \end{scope}%
      \end{scope}%
      \end{tikzpicture}%
    }%
  }%
  %
  % Preserve the original horizontal advance.
  %
  \kern-\kleinbottle@dx\relax
  %
  \endgroup
  \egroup
}

\endinput
