1
%--------------------------------------------------------------------------------------------------------- %
\documentclass[english]{book}
%--------------------------------------------------------------------------------------------------------- %
%
\usepackage[fleqn]{amsmath} %
\usepackage{amsthm,amssymb}
\usepackage[skins,theorems,most]{tcolorbox} %
\usepackage{newtxtext} %
\usepackage{setspace} %
\usepackage{xcolor} %
\usepackage{colortbl} %
\usepackage{stackengine} %
\usepackage[paperwidth=170mm,paperheight=240mm,textwidth=132mm,lmargin=12.5mm,rmargin=12.5mm,bindingoffset=12.5mm,noheadfoot,nomarginpar,showframe,showcrop]{geometry} %
\usepackage[a4,frame,cam,center]{crop} %
\usepackage{layout} %
\usepackage{graphicx} %
\usepackage[skins,theorems,most]{tcolorbox} %
\usepackage[T1]{fontenc}

\setlength{\unitlength}{1mm} %

\begin{document}

\setstretch{1.10}

I wish to box the following phrases:

-------------------------------------------------------------------------------------------------------

\noindent \hspace{36pt} \hspace{1.2pt}\fbox{over the moon}\hspace{1.2pt}, \hspace{1.5pt}\fbox{a mop up} \hspace{1.2pt}and \hspace{1.2pt}\hspace{1.2pt}\fbox{just in time}

-------------------------------------------------------------------------------------------------------

\noindent \hspace{0.8pt}so that the tops and bottoms of the first two boxes are at the same horizontal level as those \\ of \hspace{1.2pt}\textquotedblleft\hspace{1.2pt}just in time\hspace{1.2pt}\textquotedblright.

\noindent [boundary and fill colours would be nice, too]

\end{document}
leandriis
  • 62,593

2 Answers2

1

Like this ?

I simply adapted my answer here with tcolorbox: https://tex.stackexchange.com/a/458876/138900

\newtcbox{\entoure}[1][yellow]{on line,colback=#1,colframe=#1!50!black,sharp corners,right skip=1.2pt,left skip=1.2pt,
before upper={\rule[-3pt]{0pt}{10pt}},boxrule=1pt,
boxsep=0pt,left=2pt,right=2pt,top=2pt,bottom=.5pt}

screenshot

%--------------------------------------------------------------------------------------------------------- %
\documentclass[english]{book}
%--------------------------------------------------------------------------------------------------------- %
%
\usepackage[fleqn]{amsmath} %
\usepackage{amsthm,amssymb}
\usepackage[skins,theorems,most]{tcolorbox} %
\usepackage{newtxtext} %
\usepackage{setspace} %
\usepackage{xcolor} %
\usepackage{colortbl} %
\usepackage{stackengine} %
\usepackage[paperwidth=170mm,paperheight=240mm,textwidth=132mm,lmargin=12.5mm,rmargin=12.5mm,bindingoffset=12.5mm,noheadfoot,nomarginpar,showframe,showcrop]{geometry} %
\usepackage[a4,frame,cam,center]{crop} %
\usepackage{layout} %
\usepackage{graphicx} %
\usepackage[skins,theorems,most]{tcolorbox} %
\usepackage[T1]{fontenc}

\setlength{\unitlength}{1mm} %
\usepackage{tcolorbox}

\newtcbox{\entoure}[1][yellow]{on line,colback=#1,colframe=#1!50!black,sharp corners,right skip=1.2pt,left skip=1.2pt,
before upper={\rule[-3pt]{0pt}{10pt}},boxrule=1pt,
boxsep=0pt,left=2pt,right=2pt,top=2pt,bottom=.5pt}

\begin{document}

\setstretch{1.10}

I wish to box the following phrases:

-------------------------------------------------------------------------------------------------------

\noindent \hspace{36pt} \hspace{1.2pt}\fbox{over the moon}\hspace{1.2pt}, \hspace{1.5pt}\fbox{a mop up} \hspace{1.2pt}and \hspace{1.2pt}\hspace{1.2pt}\fbox{just in time}

\medskip

\noindent \hspace{36pt} \entoure{over the moon}, \entoure{a mop up} and \entoure{just in time}

-------------------------------------------------------------------------------------------------------

\noindent \hspace{0.8pt}so that the tops and bottoms of the first two boxes are at the same horizontal level as those \\ of \hspace{1.2pt}\textquotedblleft\hspace{1.2pt}just in time\hspace{1.2pt}\textquotedblright.


\noindent [boundary and fill colours would be nice, too]

\end{document}
AndréC
  • 24,137
0

You can surely do it with tcolorbox, although if you want just to do visual formatting and you do not need to manage line and page breaks, you can use plain tikz too (the trick is using baseline and anchor=base, as you can see for example here and here):

\documentclass[]{book}
\usepackage{xcolor} %
\usepackage[T1]{fontenc}
\usepackage{tikz}

\tikzset{
    text hl box/.style={
        anchor=base,
    },
    yellow with red/.style={
        text hl box, draw=red, text=cyan, fill=yellow,
    },
    only greeny/.style={
        text hl box, draw=none, fill=green!10,
    },
    only outside/.style={
        text hl box, draw=purple,
    },
}
\newcommand{\Strut}{% a bigger \strut
    \vrule width 0pt depth 4pt height 14pt\relax
}
% handy commands
\newcommand{\letboxit}[2]{%
     \tikz[baseline] \node[#1]{#2};%
}
\newcommand{\letboxits}[2]{% add a \strut
     \tikz[baseline] \node[#1]{\strut #2};%
}
\newcommand{\letboxitS}[2]{% add a \Strut
     \tikz[baseline] \node[#1]{\Strut #2};%
}

\parindent=0pt\parskip=\baselineskip

\begin{document}

I wish to box the following phrases:

\noindent\hrulefill

\noindent \hspace{36pt}  \letboxit{yellow with red}{over the moon}, \letboxit{only greeny}{a mop up} and \letboxit{only outside}{just in time}.

If you want a box with the standard font height, use a \verb|\strut| and remove the \texttt{inner sep}:

% the inner xsep is needed for things like the "j"
\tikzset{text hl box/.style={anchor=base, inner sep=0pt, inner xsep=1pt}}

\noindent \hspace{36pt}  \letboxits{yellow with red}{over the moon}, \letboxits{only greeny}{a mop up} and \letboxits{only outside}{just in time}.

\dots and you can play with a personal sized \verb|\Strut| also:

\noindent \hspace{36pt}  \letboxitS{yellow with red}{over the moon}, \letboxitS{only greeny}{a mop up} and \letboxitS{only outside}{just in time}.

\noindent\hrulefill
\end{document}

output for the snippet above

If you need to highlight text in several lines, it's not so easy. You can study the answers here and here; or if you can/want to switch to lualatex, there is the amazing lua-ul package.

Rmano
  • 40,848
  • 3
  • 64
  • 125
  • I am asking for a box that can sense automatically when only a "p" is present (with its tail down), and when a "b" is present with its tail up, or both, or neither. – keith77777 May 15 '20 at 19:28
  • ... Ok, and do what? In my example the baseline of the three boxes are aligned, and the size take into account descender and ascender (plus the box inner sep). I do not understand what is the output you want... – Rmano May 15 '20 at 23:14
  • I changed my answer --- it was that what you looked for? – Rmano May 16 '20 at 08:18
  • If you apply precisely the same box to all phrases, then the words will remain aligned horizontally, but the boxes will err up or down according to the presence of "p" or a "b" type letter. Surely, it requires one variable parameter to cater for the option? – keith77777 May 16 '20 at 11:46
  • The following two of four commands do work but are cumbersome. \newtcbox{\addnone}[1][red]{on line, arc=0pt,outer arc=0pt,colback=yellow!40!white,colframe=red!50!white, boxsep=0pt,left=2pt,right=2pt,top=2.2pt,bottom=2pt, boxrule=0pt,bottomrule=0.7pt,toprule=0.7pt}

    \newtcbox{\addupanddown}[1][red]{on line, arc=0pt,outer arc=0pt,colback=yellow!40!white,colframe=red!50!white, boxsep=0pt,left=2pt,right=2pt,top=4pt,bottom=4pt, boxrule=0pt,bottomrule=0.7pt,toprule=0.7pt} Can these four be made into one command?

    – keith77777 May 16 '20 at 11:53
  • I don't understand. In the second case and third case, with \strut, both baseline and boxes are aligned. of course, the box without the border is smaller... – Rmano May 16 '20 at 13:27