texstudio-org / texstudio

TeXstudio is a fully featured LaTeX editor. Our goal is to make writing LaTeX documents as easy and comfortable as possible.
http://www.texstudio.org/
GNU General Public License v3.0
2.81k stars 345 forks source link

List and repetition check for locally defined labels #3741

Closed pizdjuk closed 3 months ago

pizdjuk commented 3 months ago

In big documents with lot of theorems and equations it is convenient to use locally defined labels. I use some snipped like

\newcommand\locallabel[1]{\label{\currentprefix:#1}}
\newcommand\localeqref[1]{\eqref{\currentprefix:#1}}
\newcommand\localref[1]{\ref{\currentprefix:#1}}

Within a theorem I define a currentprefix like

\begin{lemma}
\def\currentprefix{some-lemma}
....
\begin{proof}

\begin{align}
\locallabel{eq1}
x=y
\end{align}

We now use the equation \localeqref{eq1}
\end{proof}
\end{lemma}

The proofs often are bigger then one-two screens. And defining a new local label I must assure that there is no other locallabels with the same name defined.

Right now the Texstudio only checks the global labels for if there are multiple defined labels. Is there a possibility to "locally" check some labels for repetitions?

sunderme commented 3 months ago

That is what cwl are for, see https://github.com/texstudio-org/texstudio/wiki/Frequently-Asked-Questions#what-are-cwl-files-good-for-and-how-do-they-work and https://texstudio-org.github.io/background.html#description-of-the-cwl-format Put the defining commands in a package (.sty) and define its usage in a cwl file of the same name.

sunderme commented 3 months ago

and seeing in the definition, that you are actually manipulating the label (from point of view of txs garbling it), that is impossible.

pizdjuk commented 3 months ago

and seeing in the definition, that you are actually manipulating the label (from point of view of txs garbling it), that is impossible.

And can you imagine what could be done? Somehow bound the lookup context to the section between \begin..\end instead of a whole document/project.

sunderme commented 3 months ago

you can do much in a program, but is it worth the effort ? It is much simpler to adapt to a workflow which is supported by txs than to reinvent the wheel with extra stuff. There is no need for \def\currentprefix{some-lemma}. How many times do you really need to type that that is worth breaking editor functionality ? (you can place in a macro if you need to) If you just enter \label{some-lemma:abc}, txs directly shows you if it was already used or not. In my opinion (other differ), it is usually not a good idea to just number stuff. Some meaningful label name goes ways to understand what you are referring to.

pizdjuk commented 3 months ago

Sometimes the formulas in the proofs have no big sense and need to be referred only one-two times. And with tons of lemmas it is not so easy to now how exactly the lemma is named, even when the names are meaningful.

How many times do you really need to type that that is worth breaking editor functionality ? (you can place in a macro if you need to)

like in every second lemma out of ~80. Before I did not use the local labels, but I was tired about labeling tons of formulas with meaningless eq_1, eq_31, etc. Because the only meaning of some "inbetween" result is just in between :) You can imagine that with a time it turned out quite ugly. So I searched for a local scope labels.

Clear, if I would start every label with a prefix from a lemma name, it would not be sooo ugly. But it's more annoying to scroll up and look "ah, the label is "lemma:separation-theorem-variant-2", then copy paste and so on for every piece, which I refer once and forget.

Also it would be other solution: that Texstudio suggests (autocompletes) the label names and references depending of label of a current scope/lemma. Like if the lemma is labeled "lemma:abc", then when I type \label{ the Texstudio autocompletes it with \label{lemma:abc} or like this. And the if I type \eqref{ then the first suggestions should come from "lemma:abc:".

I don't know, which functionality is better or easier to implement.

octaeder commented 3 months ago

Does the list of labels in the structure pane (on the left side) help? It is order in sequence of appearance of definition in the text. Do you use the actions for referencing previous/last label?

sunderme commented 3 months ago

I have provided a script in texstudio-macro which generates unique labels with upcounting and an optional prefix which can be set via a line %%prefix:...

See manual on how to download said macro. It can be found under addedFeature/UniqueLocalLabel

pizdjuk commented 3 months ago

I have provided a script in texstudio-macro which generates unique labels with upcounting and an optional prefix which can be set via a line %%prefix:...

See manual on how to download said macro. It can be found under addedFeature/UniqueLocalLabel

wow. cool! It seems pretty useful for that case. I'm trying it right now. Have an error ".....line 62: TypeError: Cannot call method 'slice' of null"

How can I debug the code? BTW, in which language are the scripts written? Lua? Ah, I found some workaround. I will use information() call to see whats happening

Does the list of labels in the structure pane (on the left side) help? It is order in sequence of appearance of definition in the text. Do you use the actions for referencing previous/last label?

@octaeder, actually no. I didn't know about this feature. The list of labels on the right is useful too. And the script from @sunderme solves the problem with unique labels. Because my document consists like of 10-15 *.tex files. And I can not know which labels was already used.

octaeder commented 3 months ago

You don't need to know about existing labels from files not opened in txs, because txs knows them. Necessary for this are following Editor settings:

image

Using a duplicate label shows different color and a tooltip:

image

You may also search usages of a label with a context menu action and then rename all of them at once if necessary.

pizdjuk commented 3 months ago

@octaeder, Yes, duplicate are shown. But sometimes I needed to try more then 3 times before it has no double :) Now I have labels like eq_3, eq_31, eq_3111, eq_311sdfdas xD

@sunderme, I correct the bug

should be like this. Because if no label is set, the lbl is set to null.

var nr = 0;
if (lbl) {
    nr=lbl.slice(prefix.length);
}

var inc=incrementStringOrSetToOne(nr);
sunderme commented 3 months ago

fixed in texstudio-macro