2

I have a certain command in LaTeX that I have used a lot in my current work:

{\bf y} # this creates a bold letter y

I want to create a shortcut e.g.

\by

that will automatically create a bold letter y. I have similar expressions I would like to simplify as my equations are getting pretty long. Any advice would be appreciated.

migrated from stackoverflow.com Aug 9 '13 at 21:22

This question came from our site for professional and enthusiast programmers.

  • Use a good editor with good shortcuts if you want to speed up your editing, don't write unreadble code. New LaTeX macros are useful to keep consistency throughout your documents, not to avoid typing. – giordano Aug 9 '13 at 14:36
  • 2
    Please throw away your LaTeX documentation: \bf has been deprecated for two decades now. – Reinstate Monica - M. Schröder Aug 9 '13 at 20:55
4

This is done with \newcommand, i.e.:

\newcommand{\by}{\textbf{y}} 

There is more functionality in it than just substituting, i.e., you can have arguments there. Please find more info and some examples here.

  • Cheers, that's great! – Jonny Phelps Aug 9 '13 at 14:14
  • Problem I'm having is that when I write $\by x$ I get both the y and the x as bold. Any way to resolve this and only have the y bold? – Jonny Phelps Aug 9 '13 at 14:28
  • Just put it in brackets, as edited. \bf is actually deprecated, so use \textbf instead – sashkello Aug 9 '13 at 14:31
  • 1
    If the command is to be used in math mode, perhaps \mathbf{y} is preferable. – egreg Aug 9 '13 at 21:39
1

\newcommand is the command you look for (see sashkello answer)

There are some things you should look for:

\documentclass{scrartcl} 
\newcommand{\by}{\textbf{y}} 

\begin{document}
Now we write a \by and a x.    
\end{document}

enter image description here

The y is direct connected to the and.

You may add a space in \newcommand to add a space before the and:

\documentclass{scrartcl} 

\newcommand{\by}{\textbf{y} } %<- space before the closing }

\begin{document}
Now we write a \by and a x.

Now we write a \by. The next sentence.

\end{document}

enter image description here

The next problem: There is an additional space when you start a new sentence.

For this problem you may use \xspace:

\documentclass{scrartcl} 
\usepackage{xspace} 

\newcommand{\by}{\textbf{y}\xspace}

\begin{document}
Now we write a \by and a x.

Now we write a \by. The next sentence.

\end{document}

The result:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.