1

Is there a way to have completion for cref references for labels defined in a command macro?

\newcommand{\midpict}[4]{    
    \begin{center}
        \begin{figure}[H]
            \begin{center}
                \includegraphics[width=#2\textwidth]{#1}
                \caption{\label{fig:#4}#3} 
            \end{center} 
        \end{figure}
    \end{center}
}

I use it like

\midpict{./template/placeholder.png}{0.5}{This is a placeholder}{placeholder-img-1}

And I'd like to use Cref{fig:placeholder-img-1} but have the completion feature for this label.

po.pe
  • 329
  • 2
    What are you doing with that macro? Why are there two center envs? and why is that not floating? IMO you are better of making a template in your editor and have it fill in this code instead of attempting to get the editor to parse an extra macro in order to find the label. – daleif Jul 03 '18 at 09:53
  • You're right, the outer center environment isn't necessary. Can you specify what you mean by "making a template in my editor"? – po.pe Jul 03 '18 at 10:08
  • Texstudio has an interface for making small templates that can be attached to a shortcut. That template will then insert the code and perhaps ask you for data to fill in the template, since I don't use texstudio I cannot provide more details, see the texstudio manual for information. What you're doing here is not generally recommended as it becomes hard to keep track up after a while. – daleif Jul 03 '18 at 10:18
  • Off-topic: The center environment will add additional spacing to your figure, better use \centering inside your figure. – samcarter_is_at_topanswers.xyz Jul 03 '18 at 10:28
  • And of course avoid H (https://tex.stackexchange.com/questions/370627/why-should-the-h-option-not-be-used-in-floats). – TeXnician Jul 03 '18 at 11:40
  • If you're willing to write the fig: part in your argument (i.e., not hide it in the definition for the \midpict command, it's possible to have the correct label be recognized for cross-referencing in TeXstudio autocompleter. Alternatively, you can write a user macro in TeXstudio that inserts that chunk of code (in \midpict) with some combination of keystrokes. – Troy Aug 04 '18 at 14:28

1 Answers1

3

Probably too late for OP, but for the record:

You can tell TeXstudio how to treat the arguments in your own commands by putting the definitions in cwl files. For your command the declaration should look something like this, if you want the label to be recognized

\midpict{imagefile}{rel. width}{caption%text}{label%labeldef}#g

However, this only works if you type the full label in the command, i.e. replace \label{fig:#4} with \label{#4} and type fig: when using the macro.

See also my answer here.

schoekling
  • 3,142