texstudio icon indicating copy to clipboard operation
texstudio copied to clipboard

Don't (auto)insert existing labels

Open dbitouze opened this issue 2 years ago • 21 comments

Describe the feature and the current behavior/state

Currently, if a user makes use of Math → Math Equations → env. equation, (s)he ends up with:

\begin{equation}\label{key}
	
\end{equation}

and, if (s)he uses it several times, (s)he has either:

  • to change the key string,
  • or to remove \label{key},
  • or to live with the “Label `key' multiply defined” and “There were multiply-defined labels” warnings.

And the same happens with the new Math → Math Definitions entries.

It would be nice for TXS to not (auto)insert existing labels and to insert instead:

  • for the first one \label{key} (or \label{key1}),
  • for the second one e.g. \label{key1} (or \label{key2}),
  • etc.

dbitouze avatar Jan 24 '23 13:01 dbitouze

so, if the user (for what ever reason) used key2 on it's own the code should generate key3 after key1?

octaeder avatar Jan 24 '23 18:01 octaeder

so, if the user (for what ever reason) used key2 on it's own the code should generate key3 after key1?

Indeed.

dbitouze avatar Jan 24 '23 18:01 dbitouze

the keys should be named. There is nothing to derive a sensible key name, unlike for example for sections with its title. So, the user needs to think of a name and put it there.

As this function has been in txs for years, I don't see any necessity to change it but someone may come up with a good solution. But spending loads of effort here is probably a waste of time (and, to follow Denis usual approach, does not teach the user to write good code/latex)

sunderme avatar Jan 24 '23 19:01 sunderme

the keys should be named. There is nothing to derive a sensible key name, unlike for example for sections with its title. So, the user needs to think of a name and put it there.

Not necessary: some users/journals want all the equations to be numbered, even the ones not subject to cross references. That's even more true for theorems(-like) environments: they have to be numbered, regardless cross references.

As this function has been in txs for years, I don't see any necessity to change it

IMO, because that's painful to have to worry about the label (change the key string or remove \label{key}) each time an equation is inserted.

but someone may come up with a good solution. But spending loads of effort here is probably a waste of time

It would take some time to TXS developers, indeed, but once, and it would save time for TXS users, many time and for the eternity :wink:

(and, to follow Denis usual approach, does not teach the user to write good code/latex)

I don't see your point.

dbitouze avatar Jan 24 '23 19:01 dbitouze

What happens in the case that you have to restructure your document? I would recommend a tool that can rename/number all labels after all work is done, and you should keep a copy of the original document for the case you want to continue working on the document. Currently renaming is demanding, for example when you decide using a different scheme how you build label names (as I did).

octaeder avatar Jan 24 '23 19:01 octaeder

@octaeder Why not, but unrelated to the current issue, isn't it?

dbitouze avatar Jan 24 '23 20:01 dbitouze

Update: sectioning and (after a change) math envs don't add (default) labels.

octaeder avatar Jun 01 '23 02:06 octaeder

@dbitouze @sunderme Let's come to details:

Generated label text (used as parameter for \label command) should consist of a fixed part called name followed by a non negative integer. Currently I work with name = "label", so we get "label0", "label1", ... The number used is the smallest that gives a unique label.

I will not change the completer, so in this case label text is "key" and it is a placeholder, as is. This is best used for individual label texts. It follows that you have to use the label item from menu LaTeX/Cross References to take advantage of this feature (this is the same as for the ref commands if you want to use their dialog presenting a label list). You may add this menu to one of your toolbars for faster access.

Questions:

  1. Should name be something different, say filename + "::"?
  2. Should name be configurable (free text or fix drop down list)? What should be in the list? Where should this be placed in the config?
  3. Should the label text be used as a placeholder?

octaeder avatar Jun 01 '23 03:06 octaeder

@dbitouze @sunderme Let's come to details:

Generated label text (used as parameter for \label command) should consist of a fixed part called name followed by a non negative integer. Currently I work with name = "label", so we get "label0", "label1", ... The number used is the smallest that gives a unique label.

Would be nice.

I will not change the completer, so in this case label text is "key" and it is a placeholder, as is.

Do you mean in the case the user afterwards inserts by hand \label?

This is best used for individual label texts. It follows that you have to use the label item from menu LaTeX/Cross References to take advantage of this feature (this is the same as for the ref commands if you want to use their dialog presenting a label list).

Sorry, I don't see your point.

You may add this menu to one of your toolbars for faster access.

Questions:

1. Should name be something different, say filename + "::"?

label⟨integer⟩ looks better. The filename is useless IMO: it is extremely rare to need the labels to be unique across files.

2. Should name be configurable (free text or fix drop down list)? What should be in the list? Where should this be placed in the config?

IMO, the name doesn't need to be configurable and I don't see the point of a drop down list.

3. Should the label text be used as a placeholder?

Why not but I guess it would be rarely used so it shouldn't be the first reached. Something like that:

\begin{equation}\label{label⟨integer⟩}
	eqn
\end{equation}

where both label⟨integer⟩ and eqn are placeholders but eqn being the one selected by default.

dbitouze avatar Jun 01 '23 05:06 dbitouze

Answers to @dbitouze questions

  1. You use the completer when you type something like \la and then choose \label{key} from the completion list (to open the list may need to press ctrl+space). The result used will not be changed by me. Since then key is a placeholder you can change it quickly to what ever you like. My change simply affects insertion of the \label command by using the main menu LaTeX/Cross References/label. So you may want to add a shortcut for it, or add it (or the whole Cross References menu) to a toolbox for quicker access (both in config dialog).

  2. What I do has nothing to do with environments. I think currently there are no such structures out there that are inserted together with a label (as it has been with equation). And if so the result is not changed by me.

  3. Previously I had following in mind: Choose from several label format options: 1. label_0, 2. filename_0, 3. xxx_0 (where xxx is given by the user).

octaeder avatar Jun 01 '23 11:06 octaeder

I prefer the following, because this solution avoids duplicating word label. The numbers are placeholders, start with 1 and use at least 4 digits:

\label{0001}, \label{0002}, \label{0003}, ..., \label{9999}, \label{10000}, ...

This solution may demand for several options to configure:

  1. spinbox: set minimum length to be used for label (by padding number with leading zeros). Default $4$.
  2. spinbox: set start number $s$. Default $1$.
  3. spinbox: step width $d$, i.e. the numbers used are $s+(k-1)d$. Default $1$.
  4. checkbox: When set then use smallest number as label that is greater than the largest number used and is not less than $s$. Otherwise use smallest number as label that is not used and is not less than $s$. Set by default.
  5. checkbox: When set then make the number a placeholder. Set by default.

These options are nothing we need urgently, but I can imagine cases where they might be useful. I think we need the 5th option. For all others use the defaults mentioned. With the placeholder you can work as before, simply the name of the placeholder changes. But if you want this numbering then deleting the placeholder each time is tedious.

@sunderme @dbitouze What do you think about this?

octaeder avatar Jun 03 '23 15:06 octaeder

the common concept usually employs eq:abc or fig:abc If you can implement this using macros, I am fine, but vanilla txs will have understandable placeholder names (here key) and in no way only a number.

sunderme avatar Jun 03 '23 17:06 sunderme

What do you think about this?

Agree with all of this.

dbitouze avatar Jun 03 '23 20:06 dbitouze

the common concept usually employs eq:abc or fig:abc

I'll open an issue (#3142) on that subject very soon.

dbitouze avatar Jun 03 '23 20:06 dbitouze

If you can implement this using macros, I am fine, but vanilla txs will have understandable placeholder names (here key) and in no way only a number.

Well, @dbitouze wished to have a sequence of numbers (we know why). And again, if you use the completer or type the command on your own, nothing will change for anyone. Furthermore when issue #3141 will be resolved, then the user can even turn back to placeholder key in case of inserting label command from menu. But this could also be done by option. Plenty of possibilities and simply one more way of creating labels when needed.

When I see how I usually reference labels (and believe me that there are many on 800 pages of math), I can say that I rarely use any list of labels. I mainly copy labels from the source or use commands that reference the previous or next label (quite nice, btw). When I want to know what the reference is, then I use the tooltip or jump to the label and then to the viewer (or backwards to find the label). Maybe using such a sequence of numbers could even free me from thinking about suitable labels (that can be lengthy), figure out the prefix I want to use, and typing or renaming labels. Just one click and I get a unique label.

I stop development now until I have commit that it is accepted, that we generate an arithmetic sequence of numbers (in one or the other way). In all other cases I create my own labels as I'm used to for many years. There will never be a solution that can predict what a sensible label should be. And typing fig: and so on doesn't bother me, because I have to type the rest of the label anyway.

octaeder avatar Jun 03 '23 23:06 octaeder

I stated my stance on this at the start of this discussion and nothing has changed. There is really no need to make things complicated here. As you are going for a menu entry, there is really no need whatsoever to change that menu entry. Just add a new macro with your functionality.

sunderme avatar Jun 04 '23 08:06 sunderme

if you go for really comfortable, you should probably take a look at https://github.com/texstudio-org/texstudio/wiki/Scripts#automatic-label-creation-by-pressing-space-after-a-structure-command

sunderme avatar Jun 04 '23 08:06 sunderme

There is really no need to make things complicated here.

I'm not sure to understand what you mean here but TXS shouldn't insert dupe labels.

dbitouze avatar Jun 04 '23 16:06 dbitouze

I'm not sure to understand what you mean here but TXS shouldn't insert dupe labels.

This is a nonsensical demand. txs inserts placeholders for the user to fill. This is common in nearly all latex editors. The placeholders are clearly marked as such and the text gives a good hint what should be filled in where. Interpreting that user support as "inserting duplicate labels" is a bit strange.

sunderme avatar Jun 05 '23 17:06 sunderme

The example given in the first OP is anything but nonsensical: placeholders inserted for the user to fill are painful since they need to pay attention to them if they are both:

  • automoatically inserted,
  • not systematically desired,
  • identical.

Typically, if I start to insert an equation I have in mind, I don't want to be disturbed by this damned (label) key I have to change (or remove) in order to not be bothered afterwards by all the duplicate keys.

But, well, TXS AFAICS currently doesn't insert such \label{key} in equations: so far, so good. (I don't know if they are other cases to consider.)

dbitouze avatar Jun 05 '23 18:06 dbitouze

But, well, TXS AFAICS currently doesn't insert such \label{key} in equations: so far, so good. (I don't know if they are other cases to consider.)

As I understand these \label{key) commands added with some environments (but which are not used) are the point in the description of this issue. They should not appear anymore. If still one exists, let me know, so we can discuss the case.

octaeder avatar Jun 05 '23 21:06 octaeder