0

Originally, I used:

\hat{x}

in my document with XeLaTeX. However, I now want to bold and italicise the alphabet with the hat. I tried the redefinition:

\AtBeginDocument{\renewcommand{\hat}[1]{\symbfit{\hat{#1}}}}

which does not work. How do I go about implementing the changes I want?

  • 2
    Never a good idea to redefine an existing command. Make a new name for it instead. Preferably a name that indicates what this actually is (I'm guessing har means something specific here). With a properly named macro your code will make a lot more sense. Additionally, then you are also free to have a different macro with bold and hat. – daleif Apr 01 '21 at 12:28
  • Thing is I don't wish to manually go through the code to change the command so I am hoping it is possible to change it at the beginning of the document. –  Apr 01 '21 at 12:30
  • It is a better choice in the long run, and perhaps the price you need to pay for not having done this to begin with. I've fixed this in many a manuscript because the author had hardcoded mathrm, mathbb, etc into the text. – daleif Apr 01 '21 at 12:32
  • You can probably use regular expressions to replace all \hats at once. You don't have to go through your code manually then, – Jasper Habicht Apr 01 '21 at 12:37
  • How do I go about doing that? –  Apr 01 '21 at 12:38
  • 1
    The suggestion by @JasperHabicht is, I think, for your editor to replace every instance of \hat with \myhat. Then, define \myhat in your preamble in terms of \hat and you should be good to go (unless you have other document macros named \hathaway or something beginning with \hat). – Steven B. Segletes Apr 01 '21 at 12:50

2 Answers2

1

I would not recommend redefining a standard command like \hat (and since you want to redefine \hat in terms of \hat, you'd have to use some tricks anyway: Can I redefine a command to contain itself?). Whenever you redefine core commands you risk running into trouble when code beyond your control (e.g. from packages you load) uses the command assuming it has its normal definition. Collaborators might also be confused when the document output does not seem to correspond to what they would expect from the code.

Here I'd just do the following.

  1. Define a new command ideally with a semantic name. Since I don't know what you are using this for, I used a non-semantic name
    \newcommand*{\bihat}[1]{\symbfit{\hat{#1}}}
    
    but you may want to look into using a name that reflects the meaning of the formatting in your context.
  2. Use your editor to replace \hat{ with \bihat{. Every editor should have a search-and-replace function. With this replacement you won't even need regular expressions and fancy stuff, since the argument structure of the new macro is exactly the same as the old one.

That way you can easily change the output the next time you change your mind: Just change \newcommand*{\bihat}[1]{\symbfit{\hat{#1}}} in your preamble to

\newcommand*{\bihat}[1]{\symbfit{#1}}

say.

moewe
  • 175,683
  • Jasper Habicht's answer explains brilliantly how you'd use RegEx-search-and-replace here, but I felt it important to emphasise that a new macro would be the more idiomatic solution here: It can make the code easier to read, more semantic and much easier to change in the future. – moewe Apr 01 '21 at 14:07
  • You are totally right! I just use regular expressions so often that it feels almost naturally for me to search and replace stuff with it. – Jasper Habicht Apr 01 '21 at 18:15
0

Based on your other question I assume that you have the following set-up:

\documentclass{article}
\usepackage{unicode-math}
\setmainfont{Latin Modern Math}
\setmainfont{Latin Modern Roman}

And you want to replace any single character inside the macro \hat{...} by something like \hat{\symbfit{...}}.

As was noted in the comments, redefining existing macros may result in problems in the future, so I'd rather propose that you change your code.

This can be done, without the need to go through the code manually, by using a replaceemnt with regular expressions which, as far as I know, is supported by TeXstudio or Texmaker. Please consult the manual of the software about how exactly to use this.

The regular expression, that you could use to replace all (!) occurences of \hat{<one letter>} by \hat{\symbfit{<one letter>}} would be

Search pattern: \\hat\{([a-zA-Z])\}

Replacement pattern: \hat{\symbfit{$1}}

Note: Please consult the manual of the software, because the replacement pattern may be different. Some software uses \1 instead of $1 to select the first capture group of the search pattern, and sometimes you need to escape \ using \\ in the replacement pattern as well.