GeneralDifference between Command and Declaration for Type Faces

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Difference between Command and Declaration for Type Faces

Post by svend_tveskaeg »

Hi all.

At this question, Thorsten mentioned that \textbf{...} is for "short text" and \bfseries is for "longer text passages"---can anyone comment a bit more on when to use what? For quite some time, I have been wondering about this.

Thank you in advance!
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

Difference between Command and Declaration for Type Faces

Post by localghost »

I think that you will find all necessary information if you follow the (second) link in my reply there. In LaTeX related reading you will not find a much different explanation.

For long text it is more convenient to use the \bfseries declaration. You will not have to bother about opening or closing braces. The \textbf command comes in handy if you only want to print a few words in another type face. So you can easily limit the scope of the declaration. The command is often used in other commands to tag short text. For example, I often use this to write names of persons in small capitals.

Code: Select all

\newcommand*{\name}[1]{\textsc{#1}}
This is a bit about semantic.

Perhaps it helps to take a look at the definition of the \textbf command in the LaTeX kernel. At first there are the commands \DeclareRobustCommand and \DeclareTextFontCommand.

Code: Select all

\def\DeclareRobustCommand{\@star@or@long\declare@robustcommand}
\def \DeclareTextFontCommand #1#2{%
  \DeclareRobustCommand#1[1]{%
    \ifmmode
      \nfss@text{#2##1}%
    \else
      \hmode@bgroup
       \text@command{##1}%
       #2\check@icl ##1\check@icr
       \expandafter
      \egroup
    \fi
  }%
}
This command is then used to define the \textbf command (and others).

Code: Select all

\DeclareTextFontCommand{\textbf}{\bfseries}
As you can see, the command is based on the declaration.


Thorsten
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Difference between Command and Declaration for Type Faces

Post by cgnieder »

Both commands are actually not meant to be used in the document directly but for use in custom commands to mark up text semantically. Suppose you have your table headings all in the same style and want to use bold face. But maybe you later decide that italics actually suit better in your document design. Since table headings typically are short text fragments, only a few words, you would define a command

Code: Select all

\newcommand*\theadformat[1]{\textbf{#1}}
in the preamble and the use it like

Code: Select all

\begin{tabular{ll}
 \theadformat{Head One} & \theadformat{Head Two} \\
 what                   & ever \\
 should be in           & this table
\end{tabular}
Although in this specific case there really is nothing against

Code: Select all

\newcommand*\theadformat{\bfseries}
and

Code: Select all

\begin{tabular{ll}
 \theadformat Head One & \theadformat Head Two \\
 what                  & ever \\
 should be in          & this table
\end{tabular}
Maybe a better example would be that you always want to mark up names in a specific way. Then you would do something like

Code: Select all

\newcommand*\name[1]{\textit{#1}}
and use it like

Code: Select all

\name{Albert Einstein} developed the theory of relativity when he was ...
On the other hand you might want to define a custom environment for quotes where you want the text to be sans serif and small. Quotes might span a paragraph, or two or three... In this case you could do something like

Code: Select all

\newenvironment{myquote}
  {\list{}{\rightmargin\leftmargin}%
     \item\relax\sffamily\small}
  {\endlist}
using the switches \sffamily\small since they're supposed to effect the whole environment.

Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Difference between Command and Declaration for Type Face

Post by svend_tveskaeg »

Nice explanations.

Thank you to both of you.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Difference between Command and Declaration for Type Faces

Post by svend_tveskaeg »

Come to think of it, should one also use

Code: Select all

\renewcommand*
instead of

Code: Select all

\renewcommand
or is the starred version for \newcommand* only?
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Difference between Command and Declaration for Type Faces

Post by cgnieder »

\newcommand, \renewcommand and \providecommand all have the same syntax. I'll try to explain them. This is the purpose of the commands:
  • \newcommand : create new command. Issue error if command already exists.
  • \renewcommand : change definition of existing command. Issue error if the command deosn't exist yet
  • \providecommand : create new command only if it doesn't exist. Do nothing if it already exists.
I'll pick \newcommand to explain the syntax:

Code: Select all

\newcommand*{csname}[number of args][value of optional arg]{definition}
The star and the args arguments are optional, so the basic use is:

Code: Select all

\newcommand\mycommand{do something cool}
(a command without argument),

Code: Select all

\newcommand\mycommand[1]{do something cool with #1}
(a command with one mandatory argument to be used like \mycommand{stuff}),

Code: Select all

\newcommand\mycommand[1][stuff]{do something cool with #1}
(a command with an optional argument to be used like \mycommand[other stuff] or \mycommand[stuff]) or

Code: Select all

\newcommand\mycommand[2][stuff]{do something cool with #1 and #2}
(a command with one mandatory argument an an optional argument to be used like \mycommand{further stuff} or \mycommand[otherstuff]{further stuff}).
The [number of args] can anything between 1 and 9.

The star will make arguments of \mycommand short (as opposed to long), i.e., they may not contain \par, neither explicitly nor implicitly. This is useful for debugging. Suppose you forgot to add the closing brace. If the command is short TeX will stop looking for it at the next occurrence of \par otherwise at the end of the file. The log entry may be more useful in the short version, so it is generally a good idea to use the starred version unless you need to be able to insert more than one paragraph as argument. The argument of \textbf{}, for example, is long.
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Difference between Command and Declaration for Type Face

Post by svend_tveskaeg »

I will have to study your answer closer later tonight or tomorrow; I am too tired to understand much at all right now, I can see. :(

Thank you, yet again, for your explanations.
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Difference between Command and Declaration for Type Faces

Post by cgnieder »

Here is an example that demonstrates the differences between long and short arguments. Uncomment the different lines to see their effects.

Code: Select all

\documentclass{article}

\newcommand\Long[1]{#1}
\newcommand*\Short[1]{#1}

\begin{document}
Text \Long{forgot closing brace
% Text \Short{forgot closing brace
% \Long{Text\par Text}
% \Short{Text\par Text}% remember that an empty line also inserts \par implicitly

The next paragraph.
\end{document}
The first line gives »Runaway Argument? File ended while scanning...«:

Code: Select all

Runaway argument?
{forgot closing brace \par The next paragraph. \end {document} 
! File ended while scanning use of \Long.
<inserted text> 
                \par 
<*> test.tex
            
I suspect you have forgotten a `}', causing me
to read past where you wanted me to stop.
I'll try to recover; but if the error is serious,
you'd better type `E' or `X' now and fix your file.

! Emergency stop.
The second line gives »Runaway Argument? Paragraph ended before...«:

Code: Select all

Runaway argument?
{forgot closing brace 
! Paragraph ended before \Short was complete.
<to be read again> 
                   \par 
l.11 
     
I suspect you've forgotten a `}', causing me to apply this
control sequence to too much text. How can we recover?
My plan is to forget the whole thing and hope for the best.
As you can see the message this time contains the number of the erroneous line.

The third line runs smoothly and the forth line gives the same error as the second one.

Regards
site moderator & package author
User avatar
svend_tveskaeg
Posts: 478
Joined: Sun Jul 12, 2009 5:31 am

Re: Difference between Command and Declaration for Type Face

Post by svend_tveskaeg »

Thank you, Clemens. It will be studied closely tomorrow. ;)
``In the game of chess, you can never let your adversary see your pieces.''
-- Zapp Brannigan, Futurama (season 1, episode 4)
Post Reply