0

I made a \newcommand that implement figures into the body of the text to make my file look neat and flow. This is the code implementation.

\documentclass[12pt]{article}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Figures packages & implementation
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\usepackage{graphicx} %Handles images.
\usepackage[labelfont=bf]{caption} %Captions - labelfond BOLD

\newcommand{\fig}[5]{
    %%%% Figure %%%%
    \begingroup
    \setlength\belowcaptionskip{0.7\baselineskip}
    \centering
    \includegraphics[height=#2, width=#3]{Images/#1}
    \captionof{figure}{#4}
    \label{fig:#5}
    \endgroup
} %The input parameters are as such:
    % #1 - image's name in folder
    % #2 - desired height
    % #3 - desired width
    % #4 - caption
    % #5 - num of figure for labeling purposes (cref etc.)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\begin{document}

\fig{example - image}{10cm}{10cm}{This is a cpation}{1}

\end{document}

This bit of code works fine, except the label {#5} is not being set as the input parameter i send, Instead the figure is labeled as fig:#5.

The idea is that each the user should be able to decide what will be the label for a certain picture through the input 5th parameter. how can i do it?

*if the original bug has no fix, an agreeable solution will be to name the label with the same name as the picture's name (i.e. the picture in the folder called Pineapple, then the label will be fig:Pineapple,

or

*the label will be numbered automatically with figure number.

Thanks for helping :>

edit: i added the execution command

10
  • 2
    don't show only snippets, always show a small but complete example. That makes it much easier to test your issue. – Ulrike Fischer Jan 5 at 13:14
  • your example is still not complete, \documentclass is missing. And we don't have "Imagename", replace this e.g. with example-image. – Ulrike Fischer Jan 5 at 13:37
  • Have a look at the easyfig package! – NBur Jan 5 at 13:53
  • Hm, you didn't test your example, or you would have realized that the path Image/ interferes and there should be no space in example-image. But beside this it works as expected. What do you think \label should do differently? – Ulrike Fischer Jan 5 at 13:54
  • 1
    @StevenB.Segletes - you are right. This is silly by me it works as you say... I was confused cause the autocompletion showed it knows only "fig:#5", so i thought the compiler does'nt get parameter #5. When i tried to cross red "fig:#5" i got the expected result (fig 1.1) so i though there is a problem. Thanks for your reply Thanks for the – Aqua- Jan 5 at 14:39
1

A solution is to use the easyfig package.

\documentclass[twoside]{memoir}
\usepackage{easyfig}
\usepackage{hyperref}
\hypersetup{colorlinks}
\usepackage{cleveref}

\begin{document}
    By default, label is \verb|fig:<picture name>|
    \Figure[placement=h!, width=.5\linewidth, caption={Image a}]{example-image-a}
    
    But user can specify the label.
    \Figure[placement=h!, width=.5\linewidth, caption={Image b}, label={fig:b}]{example-image-b}
    
    \Figure[placement=h!, min size={.5\linewidth}{!}, max size={!}{.7\textheight}]{example-image-c}
    
    \cref{fig:example-image-a} and \cref{fig:b}

\end{document}
1
  • I like the work around. I changed my code to use easyfig as it is more commpact and neat + it satisfy my needs. Thanks – Aqua- Jan 5 at 15:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.