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.83k stars 345 forks source link

Trying to achieve a special case of auto-completion for custom label/reference #1020

Closed shaaati closed 4 years ago

shaaati commented 4 years ago

Environment

What I want to achieve

I am often writing reports which contain multiple sections with a number of issues in each of them. For the issues, I am using a custom command which essentialy does the following: \newcommand{\issue}[4]{title: #1}{description: #2}{recommendation: #3}{\label{\id:#4}} Note the \id inside the label command. This allows me to use rather short labels for issues while making sure that labels stay unique across different sections. An example issue might look like this:

\section{First issue section}
\renewcommand{\id}{sec1}
\issue{blafoo}{Things are bad}{Fix things}{blafoo-label}

Referencing this issue works by typing \ref{sec1:blafoo-label}.

Until now, I manually referenced the issues by typing the \id for the section the issue is located in, plus the individual label I provided (as seen in the above code example). This is obviously very tedious, so I finally took the time to research whether I could auto-complete these references. I learned about custom cwl files and managed to specify my \issue command so that TexStudio shows valid labels when typing \ref{. However, currently only the label itself is presented for auto-completion, not the \id command that precedes the label. Referring to the example above, TexStudio would show me an auto-completion for \ref{blafoo-label}.

What I tried

My cwl file currently looks like this: \issue{title}{description}{recommendation}{%<label%>}#l I think that it would be hard, if not impossible, to make TexStudio recommend auto-completions that contain the full label sec1:blafoo-label, since this would require to know which value \id had when the label was created and include the correct value in the auto-completion. If this would be possible in any way it would be the ideal solution to my problem.

However, most of my time I will be referencing issues within a section and therefore, I would be happy if the default auto-completion would be \ref{\id:blafoo}. For the rare cases where I actually need a reference across sections I could manually change \id to the correct value. So I tried to create a new custom reference with the following line in my cwl file: \issueref{\id:%<label%>}#r This works fine for empty auto-completions, that is, TexStudio inserts \issueref{\id:label}, highlighting the word label and allowing me to easily override it. For references based on identified labels, the auto-completion still shows \issueref{blafoo-label}, completely ignoring the non-variable part that I defined. I tried different variations such as \issueref{\id}{%<label%>}#r or \issueref[\id]{%<label%>}#r, but none of these have worked so far. To make sure that all changes had been interpreted correctly, I restarted TexStudio after each change in the cwl file.

Due to my limited knowledge of cwl files I am unsure if my cwl syntax is wrong or if this might indicate a bug in the parsing and interpretation of these files. I hope anyone can help. If more information is required I could also try to create a minimal working example .tex file clarifying my problem.

muzimuzhi commented 4 years ago

As far as I know, texstudio does not have the required utility yet.

sunderme commented 4 years ago

txs will not support that kind of constructs. It can't interpret '\id' and it can't work with composite labels. In your case, best is to use macros/script to insert these constructs but there will be no support from txs concerning completer or label detection/counting or jumping to. For macros, see https://github.com/texstudio-org/texstudio/wiki/Scripts. Probably you want to type in your part of the label, press a key and let the macro put in the command.

shaaati commented 4 years ago

I understand that anything concerning the \id parameter itself cannot be supported and that it's quite a special use case.

However, the fact that self-defined references can only contain a single parameter because everything else is scrubbed from the command one defines in a cwl file seems unexpected. Especially, since it is technically possible to read labels from multi-parameter commands.

Anyway, I am currently just scratching the surface of everything that is possible with cwl files and macros. Thanks for such a useful software that helps my productivity almost daily! :)