1

I defined a command to insert figures easier and according to some college formatting. It is working just fine, but I am having a problem trying to add a conditional statement. The command is this:

%\figura{fig-location}{caption}{width(0-1)}{Source(if own, leavy empty)}{label}
     \newcommand{\figura}[5]{
        \begin{figure}[H]
            \begin{minipage}{\textwidth}
                \caption[#2]{\raggedright #2}
                \label{fig:#5}
                \begin{center}
                    \includegraphics[width=#3\textwidth]{#1}\\
                \end{center}
                \textbf{Fuente: }#4
            \end{minipage}
        \end{figure}
     }

The output is as I want it to be, here is an example:

Example of output.

However, some figures are made by me, and I don't need to cite the source of such figures. Source is the word Fuente (Spanish). So I want to modify the command with a conditional so that when I leave the #4 argument empty, it does not print the word Fuente: under the figure. Id est:

  • If #4 =, then do nothing.

  • If #4 = something, then write under the figure Fuente: #4.

Is it possible to realise the above pseudocode? If so, how can I?

I found this in another post, but it only works with the exact word "something", I need it to work with anything different from an empty argument:

\documentclass{article}
\usepackage{xstring}

\newcommand{\example}[2]{%
    \IfEqCase{#1}{%
        {}{$\sqrt{#2}$}%
        {something}{Fuente: XYZ}%
        % you can add more cases here as desired
    }
     [\PackageError{tree}{Undefined option to tree: #1}{}]%
}%
\begin{document}

\example{}{None}
\example{something}{}


\end{document}
1
  • \edef\tempa{}\edef\tempb{#4}\ifx\tempa\tempb\relax\else Fuente: #4\fi? But I would use xparse and \NewDocumentCommand so you can utilise \IfValueT{#4}{Fuente: #4} etc.
    – cfr
    Commented Oct 19, 2019 at 2:50

1 Answer 1

1

UPDATE.

I made it, here is the new version of the command:

\newcommand{\figura}[5]{
    \begin{figure}[H]
        \begin{minipage}{\textwidth}
            \caption[#2]{\raggedright #2}
            \label{fig:#5}
            \begin{center}
                \includegraphics[width=#3\textwidth]{#1}\\
            \end{center}            
              \ifstrequal{#4}{}%
              {}%
              {\textbf{Fuente: }#4}%            
        \end{minipage}
    \end{figure}
}
4
  • 1
    (i) the same result as width \caption[#2]{\raggedright #2} you will obtain with \caption{\raggedright #2}; (ii) use placement option [H] can cause bed formatting of document (empty spaces at bottom of pages); (iii) caption formatting I would rather leave to caption package settings.
    – Zarko
    Commented Oct 19, 2019 at 5:50
  • BTW, the same result as with your command definition you will obtain with shorter \newcommand{\figura}[5]{ \begin{center} \captionof{figure}{#2} \label{fig:#5} \includegraphics[width=#3\textwidth]{#1}\\ \ifstrequal{#4}{}% {}% {\raggedright\textbf{Fuente: }#4}% \end{center} }
    – Zarko
    Commented Oct 19, 2019 at 6:34
  • @Zarko tranks for your suggestions, you are right, I am having problem with big spaces at the end of some pages, do you suggest I use [htb] instead? It is mandatory that the figures and/or tables appear after they are called in the text redaction, i.e., not inmidiately below the paragrahp in with it is called, but anywhere after the paragraph.
    – Edwin
    Commented Oct 19, 2019 at 22:54
  • (i) I would not use your command but rather stick with standard way for inserting figures ... (ii) instead of [H] is better to use [ht]. H destroy one of main power of LaTeX: floating of figures/tables and with this enable to produce beautiful documents. With [ht image will appear in document at point where it is inserted, if there is enough space for it, otherwise it will be on the top of the next page.
    – Zarko
    Commented Oct 19, 2019 at 23:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .