\NeedsTeXFormat{LaTeX2e}[1995/12/01]
\ProvidesPackage{mznstyle}[2016/12/18 LaTeX package (MiniZinc for Sphinx)]

\RequirePackage{trace}
\RequirePackage[breakable]{tcolorbox}
\RequirePackage{caption}
\PassOptionsToPackage{hanging,nooneline}{caption}

\RequirePackage{chngcntr}

\RequirePackage{inputenc}
\PassOptionsToPackage{utf8}{inputenc}

\counterwithin{chapter}{part}
\renewcommand{\thepart}{\arabic{part}}

%\definecolor{UglyGreen}{rgb}{0,1,0}
\definecolor{UglyGreen}{rgb}{0.418,0.556,0.137}
%\definecolor{UglyBlue}{rgb}{0,0,1}
\definecolor{UglyBlue}{rgb}{0.273,0.508,0.703}
%\definecolor{UglyRed}{rgb}{0.2,0,0}
\definecolor{UglyRed}{rgb}{0.801,0.359,0.359}
%\definecolor{UglyOrange}{rgb}{0.2,0,0}
\definecolor{UglyOrange}{rgb}{0.930,0.906,0.664}

\DeclareCaptionFont{white}{\color{white}}


\RequirePackage{tocloft}
\PassOptionsToPackage{titles}{tocloft}

\setcounter{tocdepth}{4}
\renewcommand*\cftchapnumwidth{2em}
\renewcommand*\cftsecnumwidth{3em}
\renewcommand*\cftsubsecnumwidth{4em}

\renewenvironment{sphinxadmonition}[2]{
  \begin{tcolorbox}[colbacktitle=UglyBlue,coltitle=white,colframe=UglyBlue,fonttitle=\bfseries\large,title=#2]
}{\end{tcolorbox}}

\tcbset{boxrule=0.4pt}

\newtcolorbox[auto counter,number within=chapter]{codebox}[2][]{% 
  title=\textbf{Listing~\thetcbcounter:} #2,#1}

\renewcommand\sphinxcaption[2][\linewidth]{%
   \noindent\hb@xt@\linewidth{\hss
      \vtop{\@tempdima\dimexpr#1\relax
% don't exceed linewidth for the caption width
            \ifdim\@tempdima>\linewidth\hsize\linewidth\else\hsize\@tempdima\fi
% longtable ignores \abovecaptionskip/\belowcaptionskip, so add hooks here
% to uniformize control of caption distance to tables
            \abovecaptionskip\sphinxabovecaptionskip
            \belowcaptionskip\sphinxbelowcaptionskip
            \caption[{#2}]%
               {\strut\ignorespaces#2\ifhmode\unskip\@finalstrut\strutbox\fi}%
           }\hss}%
   \par\prevdepth\dp\strutbox
}%

\renewenvironment{sphinxVerbatim}{%
  % first, let's check if there is a caption
  \ifx\sphinxVerbatimTitle\empty
      \sphinxvspacefixafterfrenchlists
      % \parskip\z@skip
      \vskip\sphinxverbatimsmallskipamount
      % there was no caption. Check if nevertheless a label was set.
      \ifx\sphinxLiteralBlockLabel\empty\else
      % we require some space to be sure hyperlink target from \phantomsection
      % will not be separated from upcoming verbatim by a page break
          \needspace{\sphinxliteralblockwithoutcaptionneedspace}%
          \phantomsection\sphinxLiteralBlockLabel
      \fi
  \else
     \parskip\z@skip
     \if t\spx@opt@literalblockcappos
       \vskip\spx@abovecaptionskip
       \def\sphinxVerbatim@Before
           {\sphinxVerbatim@Title\nointerlineskip
            \kern\dimexpr-\dp\strutbox+\sphinxbelowcaptionspace\relax}%
     \else
       \vskip\sphinxverbatimsmallskipamount
       \def\sphinxVerbatim@After
          {\nointerlineskip\kern\dp\strutbox\sphinxVerbatim@Title}%
     \fi
     \def\@captype{literalblock}%
     \capstart
     % \sphinxVerbatimTitle must reset color
     % \setbox\sphinxVerbatim@TitleBox
     %        \hbox{\begin{minipage}{\linewidth}%
     %                \sphinxVerbatimTitle
     %              \end{minipage}}%
  \fi
  \fboxsep\sphinxverbatimsep \fboxrule\sphinxverbatimborder
  \ifspx@opt@verbatimwithframe\else\fboxrule\z@\fi
  \let\FrameCommand     \sphinxVerbatim@FrameCommand
  \let\FirstFrameCommand\sphinxVerbatim@FirstFrameCommand
  \let\MidFrameCommand  \sphinxVerbatim@MidFrameCommand
  \let\LastFrameCommand \sphinxVerbatim@LastFrameCommand
  \ifspx@opt@verbatimhintsturnover\else
      \let\sphinxVerbatim@Continued\@empty
      \let\sphinxVerbatim@Continues\@empty
  \fi
  \ifspx@opt@verbatimwrapslines
  % fancyvrb's Verbatim puts each input line in (unbreakable) horizontal boxes.
  % This customization wraps each line from the input in a \vtop, thus
  % allowing it to wrap and display on two or more lines in the latex output.
  %     - The codeline counter will be increased only once.
  %     - The wrapped material will not break across pages, it is impossible
  %       to achieve this without extensive rewrite of fancyvrb.
  %     - The (not used in sphinx) obeytabs option to Verbatim is
  %       broken by this change (showtabs and tabspace work).
    \let\sphinxVerbatimFormatLine\sphinxVerbatimFormatLineWrap
    \let\FV@Space\spx@verbatim@space
  % Allow breaks at special characters using \PYG... macros.
    \sphinxbreaksatspecials
  % Breaks at punctuation characters . , ; ? ! and / (needs catcode activation)
    \fvset{codes*=\sphinxbreaksviaactive}%
  \else % end of conditional code for wrapping long code lines
    \let\sphinxVerbatimFormatLine\sphinxVerbatimFormatLineNoWrap
  \fi
  \let\FancyVerbFormatLine\sphinxFancyVerbFormatLine
  % workaround to fancyvrb's check of \@currenvir
  \let\VerbatimEnvironment\sphinxVerbatimEnvironment
  % workaround to fancyvrb's check of current list depth
  \def\@toodeep {\advance\@listdepth\@ne}%
  % The list environment is needed to control perfectly the vertical space.
  % Note: \OuterFrameSep used by framed.sty is later set to \topsep hence 0pt.
  % - if caption: distance from last text baseline to caption baseline is
  %   A+(B-F)+\ht\strutbox, A = \abovecaptionskip (default 10pt), B =
  %   \baselineskip, F is the framed.sty \FrameHeightAdjust macro, default 6pt.
  %   Formula valid for F < 10pt.
  % - distance of baseline of caption to top of frame is like for tables:
  %   \sphinxbelowcaptionspace (=0.5\baselineskip)
  % - if no caption: distance of last text baseline to code frame is S+(B-F),
  %   with S = \sphinxverbatimtopskip (=\smallskip)
  % - and distance from bottom of frame to next text baseline is
  %   \baselineskip+\parskip.
  % The \trivlist is used to avoid possible "too deeply nested" error.
  \itemsep   \z@skip
  \topsep    \z@skip
  \partopsep \z@skip
  % trivlist will set \parsep to \parskip = zero
  % \leftmargin will be set to zero by trivlist
  \rightmargin\z@
  \parindent  \z@% becomes \itemindent. Default zero, but perhaps overwritten.
  \trivlist\item\relax
     \ifsphinxverbatimwithminipage\spx@inframedtrue\fi
     % use a minipage if we are already inside a framed environment
     \ifspx@inframed\noindent\begin{minipage}{\linewidth}\fi
     \ifx\sphinxVerbatimTitle\empty
       \begin{tcolorbox}[breakable,toprule at break=0pt,bottomrule at break=0pt,pad at break*=2mm]
     \else
       % \show\sphinxVerbatimTitle
       % \begin{codebox}{\sphinxLiteralBlockLabel \sphinxVerbatimTitle}
       \captionsetup{margin=0pt,aboveskip=0pt,belowskip=0pt,justification=raggedright,singlelinecheck=false,font=white}
       % \traceon
\begin{tcolorbox}[breakable,,toprule at break=0pt,bottomrule at break=0pt,pad at break*=2mm,title={\sphinxVerbatimTitle}]
      % \traceoff
     \fi
     % \MakeFramed {% adapted over from framed.sty's snugshade environment
     %    \advance\hsize-\width\@totalleftmargin\z@\linewidth\hsize\@setminipage
     %    }%
     % For grid placement from \strut's in \FancyVerbFormatLine
     \lineskip\z@skip
     % active comma should not be overwritten by \@noligs
     \ifspx@opt@verbatimwrapslines
       \let\verbatim@nolig@list \sphinx@verbatim@nolig@list
     \fi
     % will fetch its optional arguments if any
     \OriginalVerbatim
}
{%
  \endOriginalVerbatim
  % \par\unskip\@minipagefalse\endMakeFramed % from framed.sty snugshade
  \end{tcolorbox}
  \ifspx@inframed\end{minipage}\fi
  \global\let\sphinxLiteralBlockLabel\empty
  \global\let\sphinxVerbatimTitle\empty
  \endtrivlist
}

\def\theliteralblock{\arabic{part}.\arabic{chapter}.\arabic{literalblock}}
\def\thefigure{\arabic{part}.\arabic{chapter}.\arabic{figure}}
