% \iffalse meta-comment
%<=*COPYRIGHT>
%% Copyright (C) 2011-2012 by Martin Scharrer <martin@scharrer-online.de>
%% ----------------------------------------------------------------------
%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2005/12/01 or later.
%%
%% This work has the LPPL maintenance status `maintained'.
%%
%% The Current Maintainer of this work is Martin Scharrer.
%%
%% This work consists of the files easyfig.dtx and easyfig.ins
%% and the derived filebase easyfig.sty.
%%
%<=/COPYRIGHT>
% \fi
%
% \iffalse
%<*driver>
\ProvidesFile{easyfig.dtx}[%
%<=*DATE>
    2012/05/06
%<=/DATE>
%<=*VERSION>
    v1.1
%<=/VERSION>
    Easily include images as figures]
\documentclass{ydoc}
\GetFileInfo{easyfig.dtx}
\usepackage{easyfig}[\filedate]
\usepackage{flafter}
\EnableCrossrefs
\CodelineIndex
\RecordChanges
\OnlyDescription
\optionaloff
\begin{document}
  \DocInput{\jobname.dtx}
  \PrintChanges
  \PrintIndex
\end{document}
%</driver>
% \fi
%
% \CheckSum{0}
%
% \CharacterTable
%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%   Digits        \0\1\2\3\4\5\6\7\8\9
%   Exclamation   \!     Double quote  \"     Hash (number) \#
%   Dollar        \$     Percent       \%     Ampersand     \&
%   Acute accent  \'     Left paren    \(     Right paren   \)
%   Asterisk      \*     Plus          \+     Comma         \,
%   Minus         \-     Point         \.     Solidus       \/
%   Colon         \:     Semicolon     \;     Less than     \<
%   Equals        \=     Greater than  \>     Question mark \?
%   Commercial at \@     Left bracket  \[     Backslash     \\
%   Right bracket \]     Circumflex    \^     Underscore    \_
%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%   Right brace   \}     Tilde         \~}
%
%
% \changes{v1.0}{2011/10/31}{Initial release.}
% \changes{v1.1}{2012/05/06}{Fix of "placement" key and non-e-TeX fallback code.}
%
% \DoNotIndex{\newcommand,\newenvironment}
%
% \GetFileInfo{easyfig.dtx}
% \author{Martin Scharrer}
% \email{martin@scharrer.me}
% \ifdefined\repository
%   \repository{https://bitbucket.org/martin_scharrer/easyfig}
% \fi
%
% \maketitle
%
% \begin{abstract}\noindent
% This package aims to simplify the most common usage of \env{figure} environments
% to include a single image with a caption and label.
% \end{abstract}
%
% \section{Introduction}
% A very high percentage of all \LaTeX\ figures aim to simply display a simple image with a caption and label.
% A common way to code this is:
% \begin{lstlisting}[gobble=4,numbers=none]
%   \begin{figure}
%      \centering
%      \includegraphics[<options>]{<filename>}
%      \caption{<caption text>}
%      \label{<label>}
%   \end{figure}
% \end{lstlisting}
%
% This package provides an easy and short way to include figures like this. At the moment only single image figures
% are supported but it is planned to also support common 1x2 and 2x1 figures in future releases.
%
% \iffalse
% \section{Dependecies}
% This package uses the following packages: \pkg{xkeyval}, \pkg{adjustbox} (which uses \pkg{graphicx}) and \pkg{ifetex}.
% \fi
%
% \section{Usage}
% \subsection*{Macros}
% \DescribeMacro\Figure[<key=value pairs>]{<image filename>}
% \DescribeMacro\Figure*[<key=value pairs>]{<image filename>}
% The macro creates a \env{figure} environment where the given image file is included using \Macro\includegraphics wrapped in an \env{adjustbox} environment.
% By default the image is centered and all keys which are not known by \Macro\Figure are passed to the internal \env{adjustbox} environment. This accepts all normal
% \Macro\includegraphics keys as well as many more. The starred version uses the \env{figure*} environment instead which will insert the figure over both columns in \opt{twocolumn} mode.
%
% \noindent
% Usually this macro is used as follows:\nopagebreak
% \begingroup
% \leftmargin=0pt
% \rightmargin=0pt
% \begin{macroquote}
%   \Figure['caption='{My caption},label=fig:mypic]{myimage}\\
%   \Figure['caption='{My caption}]{myimage}\\
%   \Figure['caption='{My caption},label=fig:mypic,placement=ht]{myimage}
% \end{macroquote}
% \endgroup
%
% \DescribeMacro\easyfigdefault{<key=value pairs (before)>}
% \DescribeMacro\easyfigdefault*{<key=value pairs (after)>}
% Defines a list of key/value pairs which should be used by default. The normal version adds the pairs before the one
% given with the optional argument of \Macro\Figure, while the starred version adds them after these.
% If these macros are used more then once the old default settings are overwritten.
% This feature is similar to \Macro\adjustboxset from the used \pkg{adjustbox} package, but the keys are only affecting \Macro\Figure macros and do not stack.
% By default the \emph{before} key list is empty and the \emph{after} list is set to \Key{center} which centers the image around the \Macro\linewidth, after any other
% transformation is applied. The default centering can be deleted using \Macro\easyfigdefault*{}.
%
%
% \subsection*{Keys}
% The following keys exist to control the elements of the figure. All other keys are passed to the inner \env{adjustbox} environment from the \pkg{adjustbox} package.
%
% \DescribeKey{caption}'='<caption>
% Sets the \Macro\caption of the figure.
%
% \DescribeKey{captionabove}'='<caption>
% Sets the \Macro\caption of the figure which is placed above the figure instead of the default placement below it.
%
% \DescribeKey{shortcaption}'='<short caption>
% Sets the short version of the caption, i.e. the optional argument of \Macro\caption, used for the table-of-figures.
% This key only has an effect if either \Key{caption} or \Key{captionabove} is used as well. However, the order of keys is not meaningful.
%
% \DescribeKey{label}'='<label>
% Sets the \Macro\label of the figure. The order of this key and \Key{caption} or \Key{captionabove} is not meaningful.
% If the figure has a caption but this key is not used, a default label `\MacroArgs'fig:'<image filename>\relax' is used instead.
%
% \DescribeKey{placement}'='<placement characters>
% This key defines the requested placement of the figure. The value is then passed to the optional argument of \env{figure} or \env{figure*}.
% Valid positioning characters are `|p|' (page), `|t|' (top), `|b|' (bottom), `|h|' (here) and `|!|' (force) and any additional characters defined by packages (like `|H|').
%
% \DescribeKey{twocolumn}
% \DescribeKey{twocolumn}'=true|false'
% This boolean key will use the \env{figure*} environment which make the figure span over both columns in a \opt{twocolumn} document. It has the same effect as using \Macro\Figure*, but allows
% to switch between this two modes for all figures using \Macro\easyfigdefault. When used without a value `|true|' is assumed.
% Any other value as `|true|' switches to one-column mode, even if \Macro\Figure* was used.
%
% \subsection*{Notable \pkg{adjustbox} Keys}
% The following keys are mentioned here because they are might be often used for images inside figures. Please see the \pkg{adjustbox} manual for further details and keys.
% In addition all \Macro\includegraphics keys as mentioned in the \href{http://www.ctan.org/pkg/grfguide}{grfguide} can of course be used.
%
% \DescribeKey{center}'='<length>
% \DescribeKey{left}'='<length>
% \DescribeKey{right}'='<length>
% Aligns the image at the center or to the left or right. The \emph{optional} length defines the final width in with the image is aligned. By default the current \Macro\linewidth is used, which
% inside figures is normally identical to the \Macro\textwidth. If the image is wider than the given width it will overlap accordantly, i.e. on both sites, on the right or on the left, respectively.
% As mentioned above by default \Key{center} is applied to all figures. This replaces the normally used \Macro\centering.
%
% \DescribeKey{width}'='<length>
% \DescribeKey{height}'='<length>
% \DescribeKey{totalheight}'='<length>
% \DescribeKey{min width}'='<length>
% \DescribeKey{min height}'='<length>
% \DescribeKey{min size}'='{<length>}{<length>}
% \DescribeKey{max width}'='<length>
% \DescribeKey{max height}'='<length>
% \DescribeKey{max size}'='{<length>}{<length>}
% Can be used to set the nominal/minimal/maximum width and height of the image.
%
% \DescribeKey{frame}
% \DescribeKey{frame}'='<length>
% Adds a tight frame around the image.
%
% \DescribeKey{fbox}
% \DescribeKey{fbox}'='<length>
% \DescribeKey{fbox}'='<length>~<sep>
% Adds a frame around the image.
%
% \DescribeKey{margin}'='<all sites>
% \DescribeKey{margin}'='<left/right>~<top/bottom>
% \DescribeKey{margin}'='<llx>~<lly>~<urx>~<ury>
% This key can be used to add a margin (white space) around the image.
%
%
% \section{Examples}
%
% \subsubsection{Simple figure with an image and caption, automated label}
% A default label `|fig:image|' is used here.
% \par\smallskip\noindent
% \Macro\Figure['width=5cm,caption={An image}']{image}
% \Figure[width=5cm,caption={An image}]{image}
%
% \subsubsection{Simple figure with an image and caption, manual label}
% Normal usage with a manual label.
% \par\smallskip\noindent
% \Macro\Figure['width=5cm,caption={Second image},label=fig:myimage']{image}
% \Figure[width=5cm,caption={Second image},label=fig:myimage]{image}
%
% \subsubsection{Usage of other keys}
% Adds a frame around the image which is rotated a little.
% The placement is fixed to top or bottom.
% \par\smallskip\noindent
% \Macro\Figure['width=5cm,angle=10,position=tb,caption={Another image},frame']{image}
% \Figure[width=5cm,angle=10,placement=tb,caption={Another image},frame]{image}
%
% \StopEventually{}
% \clearpage
% \section{Implementation}
%
% \iffalse
%<*easyfig.sty>
% \fi
%    \begin{macrocode}
%<!COPYRIGHT>
\ProvidesPackage{easyfig}[%
%<!DATE>
%<!VERSION>
%<*DRIVER>
    2099/01/01 develop
%</DRIVER>
    Easy way to include figures]
%    \end{macrocode}
%
%    \begin{macrocode}
\RequirePackage{adjustbox}
\RequirePackage{xkeyval}
\RequirePackage{ifetex}
%    \end{macrocode}
%
%    \begin{macrocode}
\let\easyfig@caption\@gobble
\let\easyfig@captionabove\@gobble
\let\easyfig@placement\@empty
\let\easyfig@default\@empty
\let\easyfig@default@end\@empty
\let\easyfig@shortcaption\@empty
\def\easyfig@env{{figure}}
%    \end{macrocode}
%
% \begin{macro}{\easyfigdefault}
%    \begin{macrocode}
\newcommand*{\easyfigdefault}{%
    \@ifstar
        {\def\easyfig@default@end}%
        {\def\easyfig@default}%
}
\easyfigdefault*{center}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\easyfig@label}
%    \begin{macrocode}
\def\easyfig@label#1{%
    \@ifundefined {easyfig@r@fig:#1}{%
        \label{fig:#1}%
        \global\@namedef{easyfig@r@fig:#1}{}%
    }{}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{key}{easyfig}{caption}
%    \begin{macrocode}
\define@key{easyfig}{caption}{%
    \def\easyfig@caption##1{\expandafter\caption\easyfig@shortcaption{#1}\easyfig@label{##1}}%
    \let\easyfig@captionabove\@gobble
}
%    \end{macrocode}
% \end{key}
%
%
% \begin{key}{easyfig}{shortcaption}
%    \begin{macrocode}
\define@key{easyfig}{shortcaption}{%
    \def\easyfig@shortcaption{[{#1}]}%
}
%    \end{macrocode}
% \end{key}
%
%
% \begin{key}{easyfig}{captionabove}
%    \begin{macrocode}
\define@key{easyfig}{captionabove}{%
    \let\easyfig@caption\@gobble
    \def\easyfig@captionabove##1{{%
        \@tempdima=\abovecaptionskip
        \abovecaptionskip=\belowcaptionskip
        \belowcaptionskip=\@tempdima
        \expandafter\caption\easyfig@shortcaption{#1}%
        \easyfig@label{##1}%
    }}%
}
%    \end{macrocode}
% \end{key}
%
%
% \begin{key}{easyfig}{label}
%    \begin{macrocode}
\define@key{easyfig}{label}{%
    \def\easyfig@label##1{\label{#1}}%
}
%    \end{macrocode}
% \end{key}
%
%
% \begin{key}{easyfig}{placement}
%    \begin{macrocode}
\define@key{easyfig}{placement}{%
    \def\easyfig@placement{[#1]}%
}
%    \end{macrocode}
% \end{key}
%
% \begin{key}{easyfig}{twocolumn}
%    \begin{macrocode}
\define@key{easyfig}{twocolumn}[true]{%
    \begingroup
    \def\@tempa{#1}%
    \def\@tempb{true}%
    \ifx\@tempa\@tempb
        \endgroup
        \def\easyfig@env{{figure*}}%
    \else
        \endgroup
        \def\easyfig@env{{figure}}%
    \fi
}
%    \end{macrocode}
% \end{key}
%
% \begin{macro}{\Figure}
%    \begin{macrocode}
\newcommand{\Figure}{%
    \begingroup
    \@ifstar
        {\def\easyfig@env{{figure*}}\@Figure}%
        {\@Figure}%
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\ifetex
%    \end{macrocode}
%
% Efficient $\epsilon$-\TeX\ definition:
%
% \begin{macro}{\easyfig@expandonce}
%    \begin{macrocode}
\def\easyfig@expandonce#1{%
    \expandafter\unexpanded\expandafter{#1}%
}
%    \end{macrocode}
% \end{macro}
%
%
% \begin{macro}{\@Figure}
%    \begin{macrocode}
\newcommand{\@Figure}[2][]{%
    \edef\@tempa{%
        \noexpand\setkeys*{easyfig}{%
            \easyfig@expandonce\easyfig@default,%
            \unexpanded{#1},%
            \easyfig@expandonce\easyfig@default@end
        }%
    }%
    \@tempa
    \edef\@tempa{%
        \noexpand\begin\easyfig@env%
        \easyfig@placement
    }%
    \@tempa
    \easyfig@captionabove{#2}%
    \edef\@tempa{\noexpand\begin{adjustbox}{\easyfig@expandonce\XKV@rm}}%
    \@tempa
        \includegraphics{#2}%
    \end{adjustbox}%
    \easyfig@caption{#2}%
    \expandafter\end\easyfig@env%
    \endgroup
}
%    \end{macrocode}
% \end{macro}
%
%    \begin{macrocode}
\else
%    \end{macrocode}
%
% Fall-back definition for distributions which do not support $\epsilon$-\TeX:
%
% \begin{macro}{\@Figure}
%    \begin{macrocode}
\newcommand{\@Figure}[2][]{%
    \def\@tempa{\setkeys*{easyfig}}%
    \expandafter\toks@\expandafter{\easyfig@default,#1}%
    \expandafter\expandafter\expandafter\toks@
    \expandafter\expandafter\expandafter{\expandafter\the\expandafter\toks@\expandafter,\easyfig@default@end}%
    \expandafter\@tempa\expandafter{\the\toks@}
    \edef\@tempa{\noexpand\begin\easyfig@env}%
    \expandafter\@tempa\easyfig@placement
    \easyfig@captionabove{#2}%
    \def\@tempa{\begin{adjustbox}}%
    \expandafter\def\expandafter\@tempb\expandafter{\expandafter{\XKV@rm}}%
    \expandafter\@tempa\@tempb
        \includegraphics{#2}%
    \end{adjustbox}%
    \easyfig@caption{#2}%
    \expandafter\end\easyfig@env%
    \endgroup
}
%    \end{macrocode}
% \end{macro}
%
%
%    \begin{macrocode}
\fi
%    \end{macrocode}
% \iffalse
%</easyfig.sty>
% \fi
%
% \Finale
\endinput
