# Define a new command in a declarative way"

There are commands that look like pressing a button in MS Word(I have Windows...). For example \textbf{small bold text} compared with {\bf lengthly bold text } My question is : Is it posible to define a new command in a declarative way like \bf or redifine an old one in this style?

• Note: \bf is deprecated, the current command is \bfseries. Jan 7, 2016 at 16:22
• Also, it's better to avoid redefining an existing command unless you know what you're doing (and in some cases, it's not even possible). Jan 7, 2016 at 16:24
• Which button is that in Word? Jan 7, 2016 at 16:27
• As \bf (or the better \bfseries) is defined as you want it, it is obviously possible. But it is rather unclear what exactly you want to achieve, Jan 7, 2016 at 16:31
• It is not clear at all what you mean by "declarative". Do you just mean that changes some setting in the current scope instead of applying to an argument? If so it can only work for commands that change typesetting parameters... Jan 8, 2016 at 22:55

Yes you can, you simply need not to define arguments. Although different methods might be in order depending on the command. For the sake of the question, here's a command that turns the text bold and italics, together.

Note the {} around the command and the text. Without those, the bold and italics will continue until the document ends.

Also, \bf is deprecated. In new Latex documents, you must use \bfseries.

\documentclass{article}

\newcommand\bfandit{%
\itshape\bfseries%
}

\begin{document}

Text as it should be.\medskip

{\bfandit No, wait, not bold and italics together!}\medskip

Ok, now it's better.
\end{document}

• You should not end lines with \\  except in special contexts such as tabulars, arrays etc. Don't you get bad boxes with this? (I do.)
– cfr
Jan 8, 2016 at 2:07
• @HoodChatham But that is not true. Did you try it? You will get some textmore text just as you should because the macro will gobble the following space as it usually does. I don't know why you would need a comment sign there - I would not have thought you did. (Although it does no harm.)
– cfr
Jan 8, 2016 at 2:10
• Alenanno Thanks for the answer! Both \bfseries and \itshapes are declarative so is their combination. My question is very general. I would like to write {\url google.com} instead of \url{google.com} etc. My opinion is that if a command use a lengtly argument then is better to be defined in declerative way. So my question is : How one could define such commands ? Jan 8, 2016 at 5:52
• @HoodChatham It is a space following a macro which gets eaten. That doesn't mean that spaces within the macro get eaten. So if you define a macro to include a space, using the macro will produce that space. Try \def\donothing{} a b a\donothing b \def\dosomething{ c} a\dosomething b. You don't need \ignorespaces for the macro to gobble a following space. (And a new line is just a space, of course.)
– cfr
Jan 8, 2016 at 21:56
• @HoodChatham I don't know the TikZ code. But I understand that it uses spaces in ways which are different and important. This is why TikZ is fundamentally incompatible with the l3 syntax, for example. But, as I say, I don't know whether the comments in the code are needed or not. In any case, they do no harm, and it is better to include unnecessary comments than miss necessary ones, so including them is not a bad precautionary principle ;).
– cfr
Jan 9, 2016 at 0:58

Imagine you have some command \mycommand{...} and you insist on using a syntax {\mybutton ...} rather. You can try this. Will not always work (for example for verbatim-like commands). The outer braces are mandatory, and \foo{\mybutton ...} will not work, use \foo{{\mybutton...}} (and keep fingers crossed.)

\documentclass{article}
\usepackage{color}

\makeatletter
\newcommand\mybf {\iftrue\expandafter\mybf@\expandafter{\else}\fi}
\newcommand\mybf@ [1]{\textbf{{#1}}\egroup}

\newcommand\myunderline {\iftrue\expandafter\myunderline@\expandafter{\else}\fi}
\newcommand\myunderline@ [1]{\underline{{#1}}\egroup}

\newcommand\myred  {\iftrue\expandafter\myred@\expandafter{\else}\fi}
\newcommand\myred@ [1]{\textcolor{red}{{#1}}\egroup}

\makeatother

\begin{document}

{\myunderline Hello Beautiful} World

{\mybf Hello Beautiful} World

{\mybf\myunderline Hello Beautiful} World

{\mybf\myunderline\myred Hello Beautiful} World

{\myred\myunderline\mybf Hello Beautiful} World

\end{document}


• ManYY Thanks! This is what I was looking for! A question: Why \bfseries works qood even without braces(like pressing a button)? You just write \bfseries and everhing after that is bold! What is the mechanism behind? Another question: What do you mean by verbatim-like commands? Jan 9, 2016 at 16:57
• At its innermost core, TeX has primitives to handle fonts. \bfseries is a LaTeX command, which, in combination with many other user-hidden LaTeX commands tells to switch font, keeping most characterstics apart from using bold face. For more, you can type texdoc fntguide for a start. My answer is only a hack to convert generically some commands with one argument \foo{...} into the form {\foo ...}. This will not work in all contexts.
– user4686
Jan 9, 2016 at 17:36
• There is a notion of "catcode" in TeX which you probably don't want to hear more about. Suffice it to say that some macros rely on changing catcodes and if you grab the argument too early it will then be too late. LaTeX has the command \verb which does that. Admittedly its syntax is \verb|..| or \verb+..+ or whatever in place of | and +`, thus does not really fall into the set-up of my answer.
– user4686
Jan 9, 2016 at 17:39
• If I understand well, you say that it is impossible to define a function like \bfseries (i.e. \foo text ...) without any need for brackets ? For example something like \redtext.....\blacktext ....(no need of { and } like pressing a button...) Jan 12, 2016 at 5:05