7

I am comparing a lot of different cases that are all following a predefined naming scheme. I would now like to replace one of the character in the name with a box to indicate that this parameter is changed for the compared cases.

This is the solution I have found so far but I am not quite happy with it.

\documentclass[a4paper, 12pt]{article} 
\usepackage{xcolor}


\begin{document}
    1818{\color{red}\framebox{X}}cBb-1day    

    \textit{1818{\color{red}\framebox{X}}cBb-1day }

    \emph{1818{\color{red}\framebox{X}}cBb-1day }

\end{document}

enter image description here

The first line is quite ok but I would like the box to be just as wide as the X (I am going to make that white in the final document).

For the next to lines I have the additional problem that I would like the box to be adjusted to the text stile (e.g. italic or whatever I might later use).

3
  • \setlength{\fboxsep}{0pt}, but it's better to use \fcolorbox
    – user31729
    Commented Apr 16, 2015 at 12:15
  • @ChristianHupfer I hope you mean \setlength{\fboxsep}{-\fboxrule} Commented Apr 16, 2015 at 12:48
  • @StevenB.Segletes: Nope, since that's too tight in my point of view. If the OP has a different opinion, he/she should change it to your proposition
    – user31729
    Commented Apr 16, 2015 at 12:51

3 Answers 3

7

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}

enter image description here

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}

enter image description here

4
  • 3
    I took the liberty of adding a straightened out macro that automatically computes the slant. I believe it isn't as original as to warrant a new answer.
    – egreg
    Commented Apr 16, 2015 at 16:47
  • @egreg Thank you for the very nice improvement. Commented Apr 16, 2015 at 16:51
  • Is there a way to do this in XeTeX / without pdfsetmatrix? Commented Jan 29, 2019 at 15:34
  • @melboiko I am unaware of a way to do it outside of pdflatex. So sorry. Commented Jan 29, 2019 at 16:09
2

A small solution, but it doesn't look very nice ;-)

\documentclass[a4paper, 12pt]{article} 
\usepackage{xcolor}

\setlength{\fboxsep}{0pt}

\begin{document}
1818{\color{red}\framebox{X}}cBb-1day    

\textit{1818{\color{red}\framebox{X}}cBb-1day }

\emph{1818{\color{red}\framebox{X}}cBb-1day }

\end{document}

Some improved version for slanted boxes (and text, but not italic as requested)

\documentclass[a4paper, 12pt]{article} 
\usepackage{xcolor}
\usepackage{xparse}
\usepackage{tikz}

\NewDocumentCommand{\namingscheme}{D<>{}O{white}mO{X}}{%
  \begingroup
  \setlength{\fboxsep}{0pt}
  \tikz\node[#1] {1818\textcolor{#2}{\fbox{#4}}#3};
  \endgroup
}


\begin{document}
1818{\color{red}\framebox{X}}cBb-1day    

\textit{1818{\color{red}\framebox{X}}cBb-1day }

\emph{1818{\color{red}\framebox{X}}cBb-1day }

\textit{\namingscheme<xslant=0.15>[red]{cBb-1day}}

\end{document}

enter image description here

Disclaimer: I stole the code for the slanted text from an answer by Gonzalo Medina to one of my questions: Text from different viewports

6
  • That is a good solution for the first part of my problem. Do you also have an idea how to get the box to be e.g. italic?
    – MatoBehr
    Commented Apr 16, 2015 at 12:22
  • @MatoBehr: Oh sorry, that italic stuff is a little bit weird to get. Perhaps with TiKZ, but it might require a 'design' change of the macro etc. How should the box change if bold or \texttt is used? ;-)
    – user31729
    Commented Apr 16, 2015 at 12:24
  • For the bold font I just imagined the border of the box to be thicker. But I don't think I will need that option. Same is true for the italic if it requires TikZ or any other kind of extended code. I can just use a red 'X' as a placeholder. That will work too, just won't be as nice.
    – MatoBehr
    Commented Apr 16, 2015 at 12:41
  • @MatoBehr: See the update ... somewhat slanted ;-)
    – user31729
    Commented Apr 16, 2015 at 12:42
  • I think your update is a good idea. The thing I have not managed however it to use it within a block of normal text (it seams to be above the normal baseline). Also I think all of the text is now slanted. So the box still collides with the top of the '8'.
    – MatoBehr
    Commented Apr 16, 2015 at 12:55
1

Here is a simple solution for only the non-italic text. I think it also looks fine with italic text, but maybe you disagree.

\documentclass{article}
\usepackage{amssymb}
\begin{document}
sample$\Box{}$text

\textit{sample$\Box{}$text}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .