Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
    
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. –  Martin Schröder Aug 9 '13 at 20:55
add comment

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

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

2 Answers

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.

share|improve this answer
    
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
add comment

\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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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