2

I want to write my own library. How could one define a new command equivalent to the following ?

\par\noindent\rule[5pt]{\textwidth}{1pt}
Bhanat
  • 1
  • 4
  • Welcome, you can wrap most macro sequences into a newcommand macro. In your case e.g. \newcommand{\commandname}{\par\noindent\rule[5pt]{\textwidth}{1pt}} where you can exchange \commandname with your preference. If that makes sense with your code, is another question, because we don't know your exact use case. Instead of posting just a code snippet, a compilable MWE, which shows your aims, would help. – lukeflo Aug 12 '23 at 05:44
  • commandname do not allow hyphens - or underscores _ ? – Bhanat Aug 12 '23 at 06:28
  • Digits, too. As is generally the case with LaTeX. They are case sensitive, however, so you can some things with CamelCase. – Ingmar Aug 12 '23 at 06:40
  • No digits as well ? – Bhanat Aug 12 '23 at 06:42
  • The resulting names then end up looking like the trains in india ! – Bhanat Aug 12 '23 at 07:11
  • Not necessarily, you can also use very short command names, as long as they're not already defined. In this case newcommand will give a warning/error. – lukeflo Aug 12 '23 at 07:15
  • 4
    @Bhanat, my impression is, you try Latex unsystematically. May I ask you: which (introductory) books are you reading about Latex? – MS-SPO Aug 12 '23 at 07:35
  • 2
    What about going through https://www.learnlatex.org ? – Rmano Aug 12 '23 at 08:42

1 Answers1

2

The general answer is the one from my comment:

you can wrap most macro sequences into a newcommand macro. In your case e.g. \newcommand{\commandname}{\par\noindent\rule[5pt]{\textwidth}{1pt}} where you can exchange \commandname with your preference.

At the regular user level command names can't contain hyphens, underscores an other special characters.

Of course, there are many possibilities more to define new commands, like \NewDocumentCommand, simple \def or expl3 variants.

But for your simple use case \newcommand seems best for me.

lukeflo
  • 1,555
  • 1
    (And that‘s all documented in a good book introducing into Latex. Sigh …) – MS-SPO Aug 12 '23 at 07:43
  • 2
    And, not to mention, several good and detailed documentation's, countless posts on TeX.SE and other webpages, as well as further books. Of course, my answer is just one obvious possibility. – lukeflo Aug 12 '23 at 08:38
  • 1
    I read a book, but I am extremely old (pre-latex, pre-ams) and things have changed too much since then. – Bhanat Aug 12 '23 at 08:50
  • 1
    Ok, was it about TeX? // Please select any of the books I posted in my previous comments to your questions, be it in-print or electronic. The basic ideas of LaTeX aren't that difficult, even brilliant in a way. The problem comes from its variety; packages both simplify and complicate things. But as with speaking foreign languages, once you've got used to it, things start becoming easier. – MS-SPO Aug 12 '23 at 09:52
  • Yes, it was plain tex. Is mathtools also part of the LaTeX3 Project ? – Bhanat Aug 12 '23 at 10:14
  • If you're more used to plain TeX, you could also use primitives like \def for defining new commands/macros. – lukeflo Aug 12 '23 at 10:41
  • 1
    @Bhanat you do not need to ask just look at the copyright and maintainer lines at https://ctan.org/pkg/mathtools – David Carlisle Aug 12 '23 at 10:58
  • Very well then. Felicitations. – Bhanat Aug 12 '23 at 11:51