This question is prompted by an answer I was giving here, in which I used \includegraphics{...} within a \def definition, in order to include graphics in an in-line context.
Question: How do I go about centralising the included graphic, when using in-line, without resorting to manual ways like \hspace{}? I tried with \centering, but that has no effect. I've added fbox in the example below to make it more apparent.
\documentclass{article}
\usepackage{graphicx,keystroke,scalerel}
\renewcommand{\fboxsep}{0pt}
\def\abcd{%
{\centering \fbox{\scalerel*{\includegraphics{example-image-a}}{X}}}
}
\begin{document}
This is an in-line graphic \fbox{\keystroke{\abcd}}.
This is an in-line graphic \fbox{\abcd} with some space on either side.
\end{document}
ps. I was using it in \keystroke{...} from the keystroke package, but the problem is more general, and can be described without the keystroke package. However, please do address this in your answer if it is any different from the case without using keystroke, as I would like to improve on my answer there.


\centeringis doing nothing in your example, but it isn't clear what you want it to do,\fboxfits closely around its content so there is nothing to centre? – David Carlisle Apr 17 '17 at 07:56\centeringto somehow apply to the\keystroke, not so much centering it in thefboxitself. – Troy Apr 17 '17 at 08:08