This makes use of two really nice answers. Joseph Wright at Detect which text "mode" (normal, italic, bold, etc.) is currently in use? tells how to determine the current font shape, which I use to determine if I am in italic or not.
Bruno at Shear transform a "box" shows how to slant a box.
Together, I use Bruno's \slantbox
to slant the box while retaining the italic shape of the letter, when it is determined by Joseph's method if I am in italic shape.
What I had to do was determine the slant appropriate for this font, which I ascertained as 0.24. I also set the \fboxsep
to -\fboxrule
, so that the boxed letter would take up exactly the same amount of horizontal space as the underlying letter.
EDITED to slant box while retaining italic component inside box, as witnessed by example with f
in the box.
At the end of the MWE, I show the result of the new macro \itbox{}
.
\documentclass[a4paper, 12pt]{article}
\usepackage{xcolor}
% FROM BRUNO's ANSWER AT
% https://tex.stackexchange.com/questions/63179/shear-transform-a-box/63188#63188
\newsavebox{\foobox}
\newcommand{\slantbox}[2][.5]{\mbox{%
\sbox{\foobox}{#2}%
\hskip\wd\foobox
\pdfsave
\pdfsetmatrix{1 0 #1 1}%
\llap{\usebox{\foobox}}%
\pdfrestore
}}
% FROM JOSEPH'S ANSWER AT
% https://tex.stackexchange.com/questions/31649/detect-which-text-mode-normal-italic-bold-etc-is-currently-in-use
\makeatletter
\newcommand*{\IfItalicTF}{%
\ifx\f@shape\my@test@it
\expandafter\@firstoftwo
\else
\expandafter\@secondoftwo
\fi
}
\newcommand*{\my@test@it}{it}
\makeatother
%
\def\myfbox#1{\edef\tmp{\the\fboxsep}\fboxsep=-\fboxrule\relax%
\textcolor{red}{\IfItalicTF{\itbox{#1}}{\fbox{#1}}}%
\fboxsep=\tmp\relax%
}
\def\itslant{.24}
\def\itbox#1{\slantbox[\itslant]{\fbox{\slantbox[-\itslant]{\itshape#1}}}}
\parindent 0pt
\begin{document}
1818\myfbox{X}cBb-1day \par
\textit{1818\myfbox{X}cBb-1day }\par
\emph{1818\myfbox{X}cBb-1day }\par
\emph{1818f\myfbox{f}cBb-1day }\par
\itbox{this box should be italic}
\end{document}
A simplified definition that automatically computes the slant factor of the current font (it is stored in \fontdimen1\font
); using groups we ensure that color doesn't spill out and also that \fboxsep
needn't be restored:
\makeatletter
\newcommand{\myfbox}[1]{%
\leavevmode % for \color
\begingroup
\fboxsep=-\fboxrule
\color{red}
\edef\computed@slant{\strip@pt\fontdimen1\font}%
\ifdim\computed@slant pt=\z@
% no slant, just do a simple \fbox
\fbox{#1}%
\else
\slantbox[\computed@slant]{\fbox{\slantbox[-\computed@slant]{#1}}}%
\fi
\endgroup
}
\makeatother
A full example, showing also that the width is correct (in the ff
example the ligature needs to be broken, of course).
\documentclass[a4paper,12pt]{article}
\usepackage{xcolor}
% FROM BRUNO's ANSWER AT
% https://tex.stackexchange.com/questions/63179/shear-transform-a-box/63188#63188
\newsavebox{\foobox}
\newcommand{\slantbox}[2][.5]{\mbox{%
\sbox{\foobox}{#2}%
\hskip\wd\foobox
\pdfsave
\pdfsetmatrix{1 0 #1 1}%
\llap{\usebox{\foobox}}%
\pdfrestore
}}
\makeatletter
\newcommand{\myfbox}[1]{%
\leavevmode % for \color
\begingroup
\fboxsep=-\fboxrule
\color{red}
\edef\computed@slant{\strip@pt\fontdimen1\font}%
\ifdim\computed@slant pt=\z@
% no slant, just do a simple \fbox
\fbox{#1}%
\else
\slantbox[\computed@slant]{\fbox{\slantbox[-\computed@slant]{#1}}}%
\fi
\endgroup
}
\makeatother
\newcommand{\test}[1]{%
\sbox0{#1}#1 -- \the\wd0
}
\begin{document}
\test{1818XcBb-1day}\par
\test{1818\myfbox{X}cBb-1day}
\test{\textit{1818XcBb-1day}}\par
\test{\textit{1818\myfbox{X}cBb-1day}}
\test{\emph{1818XcBb-1day}}\par
\test{\emph{1818\myfbox{X}cBb-1day}}
\test{\emph{1818f{}fcBb-1day}}\par
\test{\emph{1818f\myfbox{f}cBb-1day}}
\end{document}
\setlength{\fboxsep}{0pt}
, but it's better to use\fcolorbox
\setlength{\fboxsep}{-\fboxrule}