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 am trying to typeset keyboard shortcuts in my document, e.g. "CTRL + SHIFT + S". The Linux Libertine font family includes a nice keyboard font that I would like to use. What's the best way to use it with fontspec? I tried the following and while it works, my latex-foo isn't that great and I was wondering if there is a better way. I also tried to use \char"XXXX instead of using the unicode character directly, but that didn't work.

\newfontface{\keyboardfont}{Linux Biolinum Keyboard O}
\newcommand{\key}[1]{{\keyboardfont #1}}
\newcommand{\ctrl}{\key{}}
\newcommand{\shift}{\key{}}
\newcommand{\alt}{\key{}}
% etc.

This allows you to typeset "normal" keys by using the \key macro, but special keys all have to be set up individually. For example, you can type:

\ctrl+\shift+\key{S} 

which results in:

enter image description here

Is there a way to define a "smart" \key command that understands special keys? So that the above can be typed like this?

\key{ctrl}+\key{shift}+\key{S} 

A similar question that unfortunately doesn't provide an answer for fontspec: Keyboard Font for LaTeX

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

The following proposal is probably not the best solution. The package libertineotf offers a lot of keys accessible via commands \LKey<name> like \LKeyEnter and a command \LKey{<glyph name>}. This can be used to define a command that nearly does what you want (using etoolbox' \ifcsdef and \csuse):

\newcommand{\key}[1]{%
  \ifcsdef{LKey#1}
    {\csuse{LKey#1}}
    {\LKey{#1}}%
  }

The biggest drawback is that for some keys only the German commands exist (e.g. there is \LKeyStrg but no \LKeyCtrl) so we need to do some extra work:

\newcommand*\LKeyCtrl{\LKey{C_t_r_l}}
\newcommand*\LKeySpace{\LKey{uniE18C}}

% let German names to English ones:
\let\LKeyStrg\LKeyCtrl
\let\LKeyEntf\LKeyDel

% add Gnglish equivalents of combinations
\let\LKeyCtrlX\LKeyStrgX
\let\LKeyShiftCtrlX\LKeyShiftStrgX
\let\LKeyCtrlAltX\LKeyStrgAltX
\let\LKeyCtrlAltDel\LKeyReset

With these definitions you can use \key like this:

\key{Ctrl}+\key{Shift}+\key{S}
\key{Enter} \key{Alt} \key{F}{12}

A complete example:

\documentclass{article}
\usepackage{fontspec}
\usepackage{libertineotf}
\usepackage{etoolbox}% for \ifcsdef, \csuse
% add not directly accessible macros:
\newcommand*\LKeyCtrl{\LKey{C_t_r_l}}
\newcommand*\LKeySpace{\LKey{uniE18C}}

% let German names to English ones:
\let\LKeyStrg\LKeyCtrl
\let\LKeyEntf\LKeyDel

% add Gnglish equivalents of combinations
\let\LKeyCtrlX\LKeyStrgX
\let\LKeyShiftCtrlX\LKeyShiftStrgX
\let\LKeyCtrlAltX\LKeyStrgAltX
\let\LKeyCtrlAltDel\LKeyReset

\newcommand{\key}[1]{%
  \ifcsdef{LKey#1}
    {\csuse{LKey#1}}
    {\LKey{#1}}%
  }

% for this example only:
\parindent=0pt

\begin{document}
\key{Ctrl}+\key{Shift}+\key{S} \\
\key{ShiftCtrlX}{S} \\
\key{CtrlAltX}{S}

\key{Enter} \key{Alt} \key{F}{12} \key{Esc} \key{Del} \key{Ins} \key{Back} \key{Tab} \\
\key{arrowleft} \key{comma} \key{Win} \key{Menu} \key{AltF}{8} \key{Reset}

\key{L}\key{i}\key{n}\key{u}\key{x}\key{Space}%
\key{B}\key{i}\key{o}\key{l}\key{i}\key{n}\key{u}\key{m}\key{Space}%
\key{K}\key{e}\key{y}\key{b}\key{o}\key{a}\key{r}\key{d}\key{Space}\key{O}
\end{document}

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.