How to add utf-8 symbols correctly?
If you use a Unicode engine (xetex
or luatex
) it is just a matter of finding a font, which offers these symbols. I have expereinced the DejaVu Sans font to be pretty complete in that regard. You might not want to change the font of the whole document just for these symbols, so it becomes necessary to allocate a \newfontfamily
which I named \boxedsymbols
. Then you can locally switch fonts to print the exotic symbols.
\documentclass{article}
\usepackage{fontspec}
\newfontfamily\boxedsymbols{DejaVu Sans}
\begin{document}
Here we go: {\boxedsymbols ☐ ⌧}
\end{document}
For pdflatex
, there is the possibility to make the Unicode characters active and define them to print a symbol, that is available in the font, which looks similar. Here I use the math symbols \square
and \boxtimes
(from amssymb
). Note, that these are not the actual Unicode symbols which you enter in your document, but rather visual equivalents.
If you want to use them in math-mode as well, replace $<symbol>$
by \ensuremath{<symbol>}
. (This is one of the rare cases, where \ensuremath
is appropriate.)
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}% for \boxtimes
\usepackage{newunicodechar}
\newunicodechar{☐}{$\square$}
\newunicodechar{⌧}{$\boxtimes$}
\begin{document}
Here we go: ☐ ⌧
\end{document}
The functionality obtained with the pdflatex
solution, namely typing the symbols directly without having to switch fonts, seems appealing and can also be achieved with the xetex
/luatex
variant. Bear in mind, that this is not really a good practice, as now the characters ☐
, ⌧
are active and code is executed each time they are encountered in the text.
\documentclass{article}
\usepackage{fontspec}
\usepackage{newunicodechar}
\newfontfamily\boxedsymbols{DejaVu Sans}
\newunicodechar{☐}{{\boxedsymbols\char`☐}}
\newunicodechar{⌧}{{\boxedsymbols\char`⌧}}
\begin{document}
Here we go: ☐ ⌧
\end{document}
The output is the same as for the first variant.
You can load the wasysym
package and use these commands. Note the \
being used to preserve the space after a macro.
\documentclass{article}
\usepackage{wasysym}
\begin{document}
Do you like \LaTeX? \Square\ Yes – \Square\ No
You can answer using \CheckedBox\ or \XBox, but don't leave it empty!
\end{document}