texlab icon indicating copy to clipboard operation
texlab copied to clipboard

Adding types on command options

Open frostedpenguin opened this issue 3 years ago • 12 comments

It would be nice if we could have a feature were you could set the types of command arguments. for example

\newcommand{\titlepage}[1 : arg1 | arg2]{
\pretitle{
  \vspace*{1cm}
  \LARGE
  \includegraphics[scale=0.5]{images/#1.jpg}\\[\bigskipamount]
}
\posttitle{\vspace*{0.4cm}}
}

afterwards we can have argument suggestions and warnings whenever the types would not match

frostedpenguin avatar Sep 17 '22 08:09 frostedpenguin

Thanks for the suggestion. This sounds like an interesting idea, although I am not sure about how one would integrate this into the document without disturbing the TeX engine (for example, does it need an extra package which strips the type annotations away?). This looks very similar to Python's type hints but for LaTeX instead.

pfoerster avatar Sep 20 '22 19:09 pfoerster

@pfoerster yes I was thinking something similar to the annotations of the lua-language-server. So essentially just add the type over the command in comments and after just add a warning if it does not match.

frostedpenguin avatar Sep 25 '22 08:09 frostedpenguin

Note that unlike Lua, TeX is a macro expansion language, so every command may be rewritten by packages. There is no type information at the LaTeX level; only some builtin functions throw an error if something is not a number. As useful as type annotation can be for real script languages, I don't think it makes much (if any) sense for LaTeX and is certainly not worth the massive effort.

clason avatar Sep 25 '22 09:09 clason

@clason I just saw that tex-studio has these completion word lists (cwl) to define the completion of commands. I was just wondering if something like that could be implemented here but with special comments. For example

%% Options:{ left, right } 

So no extra commands that can be rewritten by packages as you referred.

frostedpenguin avatar Sep 25 '22 09:09 frostedpenguin

I don't see how completion is related to type-checking?

clason avatar Sep 25 '22 09:09 clason

@clason sorry I kindof went all over the place with this one.. So the main idea I had was first and foremost defining command arguments the way I demonstrated with comments so it's clearer to the user what are his options for every command.

Now in the future if this is implemented the lsp could be checking if the arguments passed in the command have been defined in the corresponding annotations. If not it can indicate a warning completely unrelated with the latex compiler, that this option has not been defined. So even if the warning is displayed it will not have any effect at runtime.

frostedpenguin avatar Sep 25 '22 09:09 frostedpenguin

Can you give a concrete example how this would look in practice? What sort of "types" should be recognized?

clason avatar Sep 25 '22 09:09 clason

Ok so consider the example I have on the original comment. The idea is that I want to make a title page that can set different images for different universities. So I just want to inform the user which universities are supported by my template as he writes the command.

%% Options:{#1: {mit, ucla}
\newcommand{\titlepage}{
\pretitle{
  \vspace*{1cm}
  \LARGE
  \includegraphics[scale=0.5]{images/#1.jpg}\\[\bigskipamount]
}
\posttitle{\vspace*{0.4cm}}
}

You are right they are not types they are annotations just simple lists of identifiers. I am not talking about int string etc.

frostedpenguin avatar Sep 25 '22 10:09 frostedpenguin

Right, that's something different. I can see that being useful to some but quite niche in general, so I'm not convinced the cost-benefit ratio is in favor of it. But I don't have to implement it ;)

clason avatar Sep 25 '22 10:09 clason

@clason I am not here to make anyone implement it or something I was just suggesting an idea of mine. Also the same system can be implemented for common packages that are currently lacking this support.

frostedpenguin avatar Sep 25 '22 10:09 frostedpenguin

Well, somebody has to implement (and then maintain) it -- especially adding this information for the (many, many) "common packages".

clason avatar Sep 25 '22 10:09 clason

@clason well just because I opened this issue doesn't mean somebody has to implement it especially if it's hard.

frostedpenguin avatar Sep 25 '22 10:09 frostedpenguin