# Scope Problem in a newcommand definition

I am a LaTeX beginner, I am having trouble trying to implement the following code

\documentclass[a4,11pt]{article}

\newcommand{\site}[1][http]{{\bfseries #1://}}

\begin{document}
\site{www.tug.com}
\end{document}


for some reason the bold effect only apply to the argument not the entire web site. My intention was that everything passed to the \site command get printed bold face, the command also excepts arguments, ftp, https, and so on.

Would someone please explain to me why the scope of \bfseries is limited to http word only?

• The option for A4 paper is a4paper, not a4 Feb 9, 2019 at 11:44

Try

\newcommand{\site}[2][http]{\textbf{#1://#2}}


\bfseries only applies until the end of the current group. The current group of \bfseries in \newcommand{\site}[1][http]{{\bfseries #1://}} is only the bit of the URL that reads http:// (because you used two pairs of braces; had you forgotten the inner pair of braces, the boldface would apply to the current group of \site, which would almost certainly mean that the bold leaks out to places where it is not supposed to be). The rest of the URL is outside of that group and gets no boldface. In the example the old definition with one argument expands to

{\bfseries http://}{www.tug.org}


Here it is easy to understand why the boldface applies to the http:// bit only. Note that the braces around the www.tug.org do not get removed because \site only takes on argument, which means that the {www.tug.org} will just be read as is and not as argument of the command.

With the new definition \site actually takes two arguments instead of one argument as before. It is therefore easily possible to make sure that the rest of the URL is in scope of the \bfseries. As noted in the comments by egreg it is preferable to use \textbf here to recover the italic correction.

\documentclass[a4paper,11pt]{article}

\newcommand{\site}[2][http]{\textbf{#1://#2}}

\begin{document}
\site{www.tug.com}
\end{document}


The macro then expands to

\textbf{http://www.tug.org}


and so the boldface applies to the entire URL.

You may want to look into the url or hyperref package for more URL handling that enables a nicer line breaking in case it is needed.

• Better yet, \textbf{#1://#2} because this adds the italic correction. Feb 9, 2019 at 11:43
• @egreg Thank you very much for the suggestion! Edited it in. Feb 9, 2019 at 11:49
• Thanks a lot @moewe for taking the time to explain it this way, you really helped me. Feb 9, 2019 at 12:18
• @TamerAdel: The correct way to thank people is to accept their answer ;-)
– user31729
Feb 9, 2019 at 15:03