0

I am using the following command to define a custom counter for different things I refer later in the document.

\usepackage{cleveref}
\newcommand{\customcounter}[2]{%
    \newcounter{#1} \setcounter{#1}{0} \crefname{#1}{#2}{#2s}
    \expandafter\newcommand\csname #1\endcsname[1]{\noindent%
        \refstepcounter{#1}\textbf{#2\arabic{#1}}\label{##1}\\%
        \makeatletter
    }
}
\customcounter{goal}{G}
\customcounter{functionalrequirement}{FR}

When referencing things, I normally get hints from my editor (visual studio code with tex workshop or TexStudio). But when using these labels defined this way, it does not pick up the labels.

I had two solutions to this:

  1. Move the labels out of the command and write them directly behind it ( so \goal{g:reach-himalaya} would become \goal\label{g:reach-himalaya}). But this did not work, as the label now pointed to the last section instead of my goal.
  2. Create a .tex file, where i print all the labels I generate again. This file is then included with \input inside a false if block. This way, the IDE picks up the labels, but the generation still works. I couldn't get this to work, as it did not let me correctly print the commands into the file

Now my question is: How could I correctly implement any of these solutions? I think that 1 is a bit cleaner code-wise and less of a workaround. On the other hand solution 2 is also interesting, as it makes the usage of the commands easier.

  • Welcome to TeX.SX! – Bobyandbob Aug 14 '18 at 10:16
  • 1
    A manual way to get TXS to recognize the labels is to just create a .cwl entry for each of your new 'counters'. For example, \goal{label}#l if you have \customcounter{goal}{G}. Read this answer and the TeXstudio user manual if you need an intro to cwl. – Troy Aug 14 '18 at 14:37
  • Thanks for the tip. But as you sad it would be manual and involve a lot of work to set that up, and not be portable to other editors. Then I would probably be better off to just write a regex that searches for my command, and writes my fake input file – joz Aug 14 '18 at 15:58
0

I found a workaround, that is very similar to the first proposed solution. I pulled the \label part out of the command definition. the code now looks like this:

\usepackage{cleveref}
\newcommand{\customcounter}[2]{%
    \newcounter{#1} \setcounter{#1}{0} \crefname{#1}{#2}{#2s} \crefformat{#1}{#2##2##1##3}
    \expandafter\newcommand\csname #1\endcsname[1]{\noindent%
        \refstepcounter{#1}\textbf{#2\arabic{#1}}##1\\%
    }
}
\customcounter{goal}{G}

and can be used with:

\goal{\label{g:reach-himalaya}}

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.