# Conditional test inside command parameter

I want to call \includegraphics with parameter width= #1\textwidth if the number i have provided thru \mycmd is >= 1 and scale= #1 otherwise. I already saw this: Can I conditionally scale an image with graphicx but i prefere to keep my code because it simpler, i dont want a repetitive code neither with \ifthenelse (repetition of the whole commande \includegraphics) i didnt succeed to use it inside the command parameter: \includegraphics[\ifthenelse...], it better to only changing the parameter inside, so i need an extensible condition test inside the command parameter, also it would be nice if i can avoid the repetition in the OR in \mytest.

PS: i tried with \ifnumequal but it does only accept natural numbers so it doesnt help.

\documentclass{report}
\usepackage{graphicx}
\usepackage{etoolbox}

\makeatletter
\def\mycmd{\@ifnextchar[{\@mycmdwith}{\@mycmdwithout}}
\def\@mycmdwith[#1](#2,#3,#4){
\ifnum#2>=1
\begin{figure}
\includegraphics[width=#2\textwidth]{#3}
\caption[#1]{#4}
\end{figure}
\else
\begin{figure}
\includegraphics[scale=#2]{#3}
\caption[#1]{#4}
\end{figure}
\fi
}

\def\@mycmdwithout(#1,#2,#3){
\ifnum#1>=1
\begin{figure}
\includegraphics[width=#1\textwidth]{#2}
\caption{#3}
\end{figure}
\else
\begin{figure}
\includegraphics[scale=#1]{#2}
\caption{#3}
\end{figure}
\fi
}
\makeatother

\begin{document}
\mycmd(1,example-image-A,test)
\mycmd[test](1,example-image-A,test)
\end{document}

• TeX won't expand your \test command in the optional argument and the macro is actually \mytest, not \test. It would be really nice if you would provide full documents, not fragments we have to extend to something useful – user31729 Apr 28 '16 at 21:24
• @ChristianHupfer i updated it. – SAM Apr 28 '16 at 21:47
• It's still no LaTeX document – user31729 Apr 28 '16 at 21:48
• Well i think the example is now complete the errors are question related. – SAM Apr 28 '16 at 21:55
• \caption is used outside of a floating environment. – GuM Apr 28 '16 at 22:03

It makes very little sense requiring something like width=1.2\textwidth: if the first item is bigger than 1, the image would be scaled to be wider than the text width. And mixing scale and width is rather dubious.

However, the customer's always right. ;-)

\documentclass{article}
\usepackage{graphicx,xparse}

\NewDocumentCommand{\mycmd}{o >{\SplitArgument{2}{,}}r()}{%
\IfNoValueTF{#1}
{\mycmdInternalNoOpt#2}
{\mycmdInternalOpt#2{#1}}%
}
\NewDocumentCommand{\mycmdInternalNoOpt}{mmm}{%
\mycmdInternalOpt{#1}{#2}{#3}{#3}%
}

\ExplSyntaxOn
\NewDocumentCommand{\mycmdInternalOpt}{mmmm}
{
\fp_compare:nTF { #1 >= 1 }
{
\includegraphics[width=#1\textwidth]{#2}
}
{
\includegraphics[scale=#1]{#2}
}
\caption[#4]{#3}
}
\ExplSyntaxOff

\begin{document}

\listoffigures

\begin{figure}
\centering

\mycmd(1,example-image-a,test)

\mycmd[testopt](0.5,example-image-a,test)

\end{figure}

\end{document}


The list of figures (to show the optional argument is honored)

The page with the images

With just one appearance of \includegraphics:

\NewDocumentCommand{\mycmdInternalOpt}{mmmm}
{
\use:x
{
\exp_not:N \includegraphics
[
\fp_compare:nTF { #1 >= 1 } { width=#1\textwidth } { scale=#1 }
]{#2}
}
\caption[#4]{#3}
}

• yes i admit it makes littles since but it's very helpful to get used to latex this way, lol at the customer's always right, answer is clear and perfect thanks. – SAM Apr 28 '16 at 22:24
• the question does ask to avoid repeating \includegraphics :-) – David Carlisle Apr 28 '16 at 22:30
• @DavidCarlisle Now you have to upvote – egreg Apr 28 '16 at 22:44
• Andrew point out earlier that it will makes the solution complicated for nothing, a complicated solution with obscure code is never welcome lol i prefere code repete than an incomprehensible code – SAM Apr 28 '16 at 22:47
• I would, but I'm not allowed to upvote my own answer. – David Carlisle Apr 28 '16 at 22:49

the requested syntax seems very odd, but width= and scale= are just variants of scaling with width implying scaling the natural width so you just need the following which avoids duplicating \includegraphics:

\documentclass{article}
\usepackage{graphicx}

\makeatletter
\newcommand\zz[1]{#1\ifdim#1pt<1pt \Gin@nat@width\else\textwidth\fi}
\makeatother

\begin{document}
\vspace*{-4cm}
\enlargethispage{4cm}
\centering

X\dotfill X

\includegraphics[width=\zz{1.2}]{example-image}

\includegraphics[width=\zz{1}]{example-image}

\includegraphics[width=\zz{0.5}]{example-image}

\end{document}