13

I'm writing a paper and I used \vec{} to express all vectors. Now, I changed my mind and want to write them in bold letters using \bf{} for instance. Is there a fast way to change all that's been written \vec{} to \bf{} without doing it for every single vector?

  • To change the appearance of an element globally is actually the key point of using a markup language over WYSIWYG software. In latter case you have to go through all vectors by hand. In the first case you could do this. But even though a search and replace routine will speed up this process there is no actual reason to do so. – Ruben Aug 1 '15 at 22:32
24

First, I have to recall that \bf is a plain TeX macro. It is not bad practice to use it but it isn't needed anymore since \bfseries was intoduced with LaTeX and its syntax is exactly the same as for good old \bf.

Second, don't change your markup! (by exploiting a search and replace routine). If you wrote \vec in your manuscript you did it because the element that you want to typeset is a vector and not because the \vec macro puts a nice little arrow above your letter.

You don't break anything if you redefine \vec since it is a simple macro that invoces \mathaccent. In fact redefining it is best practice. The shortest and most efficient way to accomplish this in your case would be

\renewcommand\vec{\mathbf}

If you want to use the original form of the \vec macro next to the redefined one you should put \let\arrowedvec=\vec (pick any name you want instead of "arrowedvec") before the line with \renewcommand\vec{\mathbf} and use \arrowedvec of course when you need the original version of \vec. Alternatively you could also define the macro directly: \def\arrowedvec{\mathaccent"017E}. The only difference here concerns memory usage which nowadays mostly isn't an issue anymore.

Addendum

I recommend to read the post Separate content from formatting - i.e., "just type". It contains a brief discussion on logical markup.

| improve this answer | |
  • 1
    i'd be inclined to first \let\oldvec\vec, just in case i ever wanted to put an arrow over some symbol and didn't remember the "long" version of the code. – barbara beeton Aug 2 '15 at 8:15
  • @barbarabeeton -- Good point! I'll add a remark... – Ruben Aug 2 '15 at 10:09
12

The proper way of doing this would be to use a search-and-replace command in your text editor in connection with a custom macro, e.g. \myvec.

However, if you insist on having a quick solution inside your document (which I discourage), you could add the following to your document's preamble:

\let\oldvec=\vec
\renewcommand{\vec}[1]{\oldvec{\mathbf{#1}}}

If you wish to have the letter printed in bold face without the vector accent on top of it, use

\renewcommand{\vec}[1]{\mathbf{#1}}

instead

| improve this answer | |
12

You could change meaning of \vec, but that would be very bad pratice. What I would do:

  1. Define new command with style like: \newcommand{\vectorstyle}[1]{\mathbf{#1}}
  2. Use Find and Replace feature of your text editor/IDE to replace \vec{ for \vectorstyle{
  3. In the event of next chage, just replace the definiton of the \vectorstyle

P.S. Do not use \bf. It's bad pratice. You can use \bfseries instead.

| improve this answer | |
  • 2
    changing the meaning of \vec isn't bad practice at all (see also my answer.) – Ruben Aug 1 '15 at 22:12
  • 3
    \bf is not bad practice: it's deprecated and it's existence cannot be relied upon. (Also it doesn't have an argument...) – cgnieder Aug 2 '15 at 4:59
6

First, {\bf ...} is obsolete. Instead it you should use \mathbf{...} or \boldsymbol, depending on which packages you use. For replacing you have two possibilities:

  • By means of the editor the 'Replace' function replaces \vec{ with for example mathbf{.
  • Redefine \vec with \renewcommand (this gives easy way to change your mind again ...:) )
| improve this answer | |
  • 9
    and even before \bf became obsolete, it was properly used as {\bf ...}, not \bf{...}. – barbara beeton Aug 1 '15 at 13:24
4

I use this macro in many of my papers:

\renewcommand{\vec}[1]{\boldsymbol{\mathrm{#1}}}

It can always be commented out, of course, if the original definition is preferred.

| improve this answer | |
0

I know this post is old, but while all the answers here answer how to do this with raw LaTeX, I believe all those answers are wrong.

Don't override a function, you might want to use the original function later for whatever reason. Furthermore, you add more maintenance, and uglier code. Instead, actually fix your document. We use raw-text documents for a reason.

You don't want to manually do this, so use a search and replace function instead. Your IDE might have a way to do this, but I would fix it from the terminal instead, as such:

If you use Linux, and/or have access to sed, run this, changing your filename:

sed 's/\\vec{\(.*\)}/\\bf{\\1}/g' -i filename.tex

Explanation:

sed(1) is a stream editor, and can search for strings in text, and replace content for you. -i automatically saves the new content to the provided stream (a file, normally) instead of outputting it.

s/FIND/REPLACE_WITH/g is the generic form of the function you put between quotation marks.

RegEx

In this case, FIND is equivalent to \vec{.*} in RegEx format. We want to capture whatever .* is equivalent to (meaning everything inside there, since . means any character, and * means {0..infinite} many of those). Since we want to capture this, we surround it with brackets, creating a named group (called \1 since it is the first group created).

So, in raw RegEx, we are finding \vec{(.*)}, saving \1 temporarily, and then replacing the whole content that matches with \vec{\1}, to \bf{whatever was here}.

Backslashing

Since we are working on the terminal with sed, we need to backslash some characters, or it will be confused. The terminal reads \, and then tries to do something with the next character. \n means newline, for instance. We don't want this, so we have to backslash the \ as \\. The same goes for \1.

sed(1) tries to match ( and ) as characters, so we want to backslash those too, so sed(1) knows we actually want to create a group instead of matching the parentheses.

Final note

You might be able to use the RegEx I provided directly in your IDE, that really depends on your setup. I explained each part so you can understand what is going on, and how to change whatever you might need to change.

The s in your sed string stands for substitute, and tells sed what to do with the following parts. The g on the end allows for multiple matches on the same line, standing for global. sed(1) usually works line by line, so it would match the first match of every line by default, but there might be more than one match per line.

On a final final note, you mentioned your matches are actually vectors, so consider using \myvec{\1} instead, and define a command for this function, as others have explained here.

| improve this answer | |
  • Several of the answers do recommend search and replace, which sed is also accomplishing. Also, it's {\bf stuff}, not \bf{stuff}. The latter will leave things bold afterward. – Teepeemm Apr 14 at 20:00
  • You are correct. The replace should be {\bf \1} instead. I forgot how \bf worked for a second. – mazunki Apr 14 at 20:08
  • Also, some did mention search and replace, but didn't provide any way of actually doing this. I provided an IDE-independent solution, forgetting that bf doesn't actually take the to-be-bold text as an argument. The OP also seemed to forget this, and might be misleading for other readers looking for similar solutions where you want \vec{foo} to \myfunction{foo}. – mazunki Apr 14 at 20:11

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.