0

I am defining some custom commands for latex documents. Do names for \def, \newcommand, and \newenvironment not accept - and _ ?

Veak
  • 1
  • 1
    environment names may but with standard settings command names must be all letters or a single non letter such as \- or \_ – David Carlisle Sep 05 '22 at 21:49
  • 1
    While there are apparently methods of making any character work (see here), there are best usage conventions, which are at least partly enforced by default, and for your own usage I think it's best to stick to letters, or those together with @ if you're writing a package or document class (where \makeatletter is on) and the commands are only used internally to it. – frabjous Sep 05 '22 at 21:55
  • Yes, I am writing a package and want the package name showing at the start as is normally done with lisp. – Veak Sep 05 '22 at 21:59
  • Would \- or \_ used in the definition only? – Veak Sep 05 '22 at 22:02
  • If I write a package, could I make commands for users that have @ or is done for internal use only. The reason I would have liked to use something like my-tool is that the words are separated and I do not need to use the shift key. – Veak Sep 05 '22 at 22:25
  • use all lowercase for document level commands – David Carlisle Sep 06 '22 at 07:53
  • https://tex.stackexchange.com/a/48207/1090 – David Carlisle Sep 06 '22 at 07:56
  • @DavidCarlisle - It would be beneficial to scrutinize the capability of allowing - in names for \def, \newcommand, \newenvironment. Things could became much more systematic, and comparable to lisp, a very appealing language in regards to naming and documentation conventions. Is there a problem hampering this change? – Veak Sep 06 '22 at 09:31
  • 2
    @Randy changing the core syntax of a language after 40 years of continuous use is bound to have consequences, almost all negative. – David Carlisle Sep 06 '22 at 09:39
  • 1
    how many millions of existing documents have \alpha-\beta or \textgamma-ray or ... Not to mention invalidating every existing latex tutorial. Note the change is trivial \catcode \-=11 will make-` a letter, but you should not do it except in controlled evironments – David Carlisle Sep 06 '22 at 09:42
  • You would still have to think about that if you want the language to evolve for the future decades. Guy Steele had discussed the importance and problems of designing an evolving language in 1998. – Veak Sep 06 '22 at 09:54
  • The problem of arithmetic subtraction with hyphenated words was also pointed with things such as recutils. Mostly because there was not much thought about hyphenated words and resulting conflicts. Been scrutinising your comment. One rule would be that names can only be inter-hyphenated, with affixed hyphens at the end being disallowed. – Veak Sep 06 '22 at 10:04
  • @DavidCarlisle With \def\galex\-pipe{textbf{|}} fallowed by \newcommand{\galex\-bfblue}[1]{\textcolor{blue}{\textbf{#1}}} gives the latex error Command \galex already defined. with l.38 ...x\-bfblue}[1]{\textcolor{blue}{\textbf{#1}}}. – Veak Sep 06 '22 at 11:41
  • If there is an actual use case then maybe it is worth to make such a change, fix all the problems that come with it, and introduce backwards incompatibility. However, if the reason is just that it conflicts with a particular naming convention and you can just as well use \mytoolCommand, \myToolCommand, \mytoolcommand, \mtcommand etc. then it is not worth all the trouble in my opinion. – Marijn Sep 06 '22 at 12:38
  • Another possibility is to use \galex@pipe, then one can change the name in there documents in case of conflicts \let\pipe\galex@pipe. – Veak Sep 06 '22 at 12:40
  • Hyphented words was not thought about in the original design. Regarding incompatibilities, development on latex for future decades has to be separate as latex3 which should be the long-term project developing the next version of latex. – Veak Sep 06 '22 at 12:44
  • Use of hyphented-words is a superior system for readability, and go a long time back, up the first programming languages such as lisp. – Veak Sep 06 '22 at 12:47
  • Yet very few programming languages allow a hyphen in the name of a variable or command, even those that have an explicit command prefix (like \ in LaTeX) such as Perl or PHP. – Marijn Sep 06 '22 at 12:51
  • But then, at a later stage, many languages introduce hacks like @ for latex to help avoid package conflicts, when hyphented words would be more natural. Could be just me, but these things have to be part of the design. Nevertheless, I am utilising @ for this rather than Camel Case. – Veak Sep 06 '22 at 13:54
  • @Randy sorry why are you telling me that code I didn't suggest is in error? the answer is really very simple use lowercase ASCII for document commands. you can can change the syntax as I show above and make - a letter allowed in command names but then essentially you are defining a domain specific language it's no longer latex – David Carlisle Sep 06 '22 at 15:58
  • @DavidCarlisle You mentioned the possibility of using \- or \_, which although not standard, can be done. Without pointing out what settings would be needed. Without that information, their introduction gets latex complaining. Although I can see that \- or \_ do not really provide an improvement, making the use of the @ way more appealing. – Veak Sep 06 '22 at 16:07
  • It is true that LaTeX does not have the concept of namespaces, and this occasionally leads to problems. You could argue that this is a shortcoming in the language design and that it should be implemented (of course you can also argue against that). However, the exact syntax of either a 'formal' namespace or a pseudo-namespace is mostly a matter of style or preference, whether it is - or @ or :: or camelcase, and much less a question of design. – Marijn Sep 06 '22 at 16:18
  • True, although one has to mention that allowing only the possibility of CamelCase makes names really tight. Have been able to work with @ though. Disallowing punctuation in names is a design decision nevertheless (historically - and _ have been a common occurrence, although - works better for developers because it separates things using standard orthographic rules and does not require use of the Shift Key). – Veak Sep 06 '22 at 16:35
  • No I said you can use a single non letter, \-and \_ are both standard latex commands that you could redefine. Or you can make - a letter so it is allowed in csnames with \catcode \-=11 ` as shown above. – David Carlisle Sep 06 '22 at 16:39
  • Certainly would need to look deeper into the use of -. Could make - into a letter within a style file perhaps. Would the definition of a letter affect things like \alpha-\beta ? – Veak Sep 06 '22 at 17:42
  • @DavidCarlisle I would prefer making - a letter. I had missed your small note about it. – Veak Sep 06 '22 at 17:50
  • It would be like \makeatletter but for -. – Veak Sep 06 '22 at 17:56
  • Would I have to reset - again in the style file after I am done with the definitions? – Veak Sep 06 '22 at 18:04
  • really, you should not. Languages have their own idioms, you can program C++ as if it were Fortran, and you can program TeX as if it were lisp, but if you want your package to have more than one user, it is better to use conventional forms. – David Carlisle Sep 06 '22 at 18:05
  • you keep asking questions with no context. @ is reset after package definitions because the @ named commands should never be used in documents. So if you want internal commands with - names you can reset -but if you want the - names to be accessible you obviously can not reset -. The constraint is not really about definitions but parsing: is \foo-bar one token or 5. – David Carlisle Sep 06 '22 at 18:10
  • Ok, In my main style file I will do \makeatletter but call \makeatother an the end of mynames.sty – Veak Sep 06 '22 at 18:27
  • @Randy in a file included by \usepackage or \RequirePackage you don't need to use \makeatletter and \makeatother, this is done for you by LaTeX. However, if you change the category code of any other character you'll have to reset it yourself at the end of your package (at least if you don't want to break any existing other package and/or document). – Skillmon Sep 06 '22 at 19:12
  • @Randy also, your suggestion to change the syntax of LaTeX is as if you'd say ( can now be part of a variable's name in Lisp. – Skillmon Sep 06 '22 at 19:13
  • I do not consider the extension so drastic. Besides the fact that hyphenated words form part of linguistic orthography. Can also see the benefit of allowing numbers as well. Hyphens could affect things like \alpha-\beta which could be fixed by not allowing hyphens at the end of names. What other things would make the change problematic, besides changes to manuals? – Veak Sep 06 '22 at 19:45
  • @Randy potential changes to every single document there is? The extension is just as drastic as saying ( is now valid in variable names in lisp. You might get lucky, because your code always used a space before and after (, but you just might not! – Skillmon Sep 06 '22 at 20:05
  • And your "not allowing hyphens at the end of names" can't be achieved without changes to TeX itself (not on the LaTeX level). There is either allow or prohibit, no in between. – Skillmon Sep 06 '22 at 20:08
  • Right. Evolving a language was once discussed by Guy Steele (1998). There are a number of people working and promoting that. Got an interest in it from Luca Saiu who had been working on extensible programming languages. The biggest objection is that after 40 years, there are few people courageous enough to freshly redesign. At any rate. it is as it has always been. – Veak Sep 06 '22 at 20:28
  • LaTeX is extensible and still gets extended, but in a backwards compatible manner. If you want to start over, fine, go ahead; but don't expect millions of documents being adapted for whatever you design. One of the big advantages of (La)TeX is backwards compatibility, so I guess you should accept that this isn't changed in the LaTeX kernel. No one is stopping you from providing a package that uses this as an internal naming convention, you might even change the whole syntax of LaTeX with your package, but in the latter case don't expect many to adapt. – Skillmon Sep 06 '22 at 21:42
  • No expectations on what millions do. Except when solicited with questions about my experience. Seen things changing but relatively slow. – Veak Sep 06 '22 at 22:11
  • Also, there are packages that introduce their own syntax, but only in a confined space (usually an environment). See for example TikZ, that drops the need of a leading \ in key words (after the first), and requires termination of paths by ;. Or tipa/tipauni which change your input to something pretty different. Or beamer with its syntax for overlays. There are packages that allow for functional programming, lambda calculus, etc. The thing about TeX is that you can tweak it until unrecognizable, but if you want others to use it as well you need to establish and keep standards. – Skillmon Sep 07 '22 at 07:52
  • Quite true that there are those possibilities as well. Customarily I do not like many packages. Have seen people doing that and it was not pretty to see what,s going on. – Veak Sep 07 '22 at 09:39

0 Answers0