27

I want to use a unicode character, say $ℝ$, and get the equivalent command $\mathbb{R}$. So what I want is something like

\newcommandℝ{\mathbb{R}} //it won't work by default, of course 

How can I do this? I know this is not related to use unicode-math package, because unicode-math package transform unicode to corresponding output, however I just want to use some unicode characters as a shorthand only (to increase the readibility).

3
  • »To increase the readability«: Maybe your editor offers replacement of some LaTeX-Code with unicode signs. You write \mathbb{R}, but after a second the editor simply displays a ℝ. Commented Mar 7, 2017 at 9:46
  • @Keks Dose I'm using TeXStudio. Does it have this feature? What editor do you suggest? Commented Mar 7, 2017 at 16:16
  • I'm using Emacs and I'm truly clueless about TeXStudio and about writing math. I've seen some kind of replacement with org-mode in Emacs. Commented Mar 8, 2017 at 18:46

2 Answers 2

29
\documentclass{article} \usepackage{amssymb} \usepackage[utf8]{inputenc} \usepackage{newunicodechar} \newunicodechar{ℝ}{\mathbb{R}} \begin{document} $ \mathbb{R} =1$ $ ℝ = 1 $ \end{document} 

For XeLaTeX you can just remove the inputenc package, but in this case is simpler the David's suggestion of use unicode-math.

3
  • Thank you all. It works perfectly. And this increase huge readability of source code. Commented Mar 5, 2017 at 14:31
  • Why in the world would you use as an example the equation saying that the set of all real numbers is equal to the number 1? ;-) Commented Dec 4, 2022 at 9:30
  • 1
    @Gaussler Because is not very exact, but is short. :-) Commented Dec 4, 2022 at 19:28
24

If you are using

\usepackage[utf8]{inputenc} 

then just add

\DeclareUnicodeCharacter{211D}{\mathbb{R}} 

With unicode TeX engines it is probably advisable to use unicode-math which would allow ℝ to be typeset directly, but if you are using 8bit math fonts still then you can use

\catcode`ℝ=13 \newcommandℝ{\mathbb{R}} 
10
  • I'm using XeLaTeX. Can I directly write down \DeclareUnicodeCharacter{211D}{\mathbb{R}} without any conflict? Commented Mar 5, 2017 at 11:38
  • Because if I use the unicode-math package, the fonts embedded in the pdf file will be changed to TTF format(I checked those by Adobe Reader). And due to the strange behavior of Illustrator, the pdf file cannot be properly modified again due to the font matter. (I use illustrator to do further fancy decorations.) And I also want something like: whenever I type ☆{}, it correspond to the command \proof{}, where \proof is a command defined by me. Commented Mar 5, 2017 at 11:43
  • What does 13 mean in your code? Commented Mar 5, 2017 at 11:48
  • @Eric but are you using opentype fonts for the text? Or are you using 8bit fonts everywhere (in which case why xetex?) Commented Mar 5, 2017 at 11:49
  • 2
    @Eric you should probably accept the other answer, egreg's package basically does one or other of the things I suggest here depending if pdftex or xetex is being used. Commented Mar 5, 2017 at 12:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.