TransWikia.com

Printing labels into file to support editor

TeX - LaTeX Asked by joz on May 31, 2021

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}
    expandafternewcommandcsname #1endcsname[1]{noindent%
        refstepcounter{#1}textbf{#2arabic{#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 goallabel{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.

One Answer

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}
    expandafternewcommandcsname #1endcsname[1]{noindent%
        refstepcounter{#1}textbf{#2arabic{#1}}##1%
    }
}
customcounter{goal}{G}

and can be used with:

goal{label{g:reach-himalaya}}

Answered by joz on May 31, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP