# How to frame text of different lines

I'm trying to reproduce this:

I was able to reproduce everything but the frame below "John sleeps":

I first tried using gb4e, but I didn't find a solution. Now, my examples are written by hand, using the tabbing environment. The code follows:

\documentclass[]{article}
\begin{document}
\begin{tabbing}
\hspace{0.8em} \hspace{6em} \= \textit{Paul} \hspace{1.5em} \=
\textit{thinks} \hspace{2.8em} \= \textit{that} \hspace{2.5em} \=
\textit{John} \hspace{1.2em} \= \textit{sleeps}\\~\\
\hspace{0.8em}$(14)$ \hspace{5em} \= $n$ \hspace{2em} \= $s/(n)[n]$
\hspace{2em} \= $n/[s]$ \hspace{3em} \= $n$ \hspace{2em} \= $s/(n)$\\
\hspace{0.8em}$(15)$ \hspace{5em} \> $n$ \hspace{2em} \> $s/(n)[n]$
\hspace{2em} \> $n/[s]$ \hspace{4.8em} \= $s$\\
\hspace{0.8em}$(16)$ \hspace{5em} \> $n$ \hspace{2em} \> $s/(n)[n]$
\hspace{5em} \= $n$\\
\hspace{0.8em}$(17)$ \hspace{5em} \> \> \= \hspace{2.5em} $s.$
\end{tabbing}
\end{document}


How can I make this frame around n and s/(n) of line 14 and s of line 15, just like the first image?

• I added the linguistics tag and dropped mdframed. The question is not specifically about mdframed (yet), but it is about an example in linguistics, so that tag might be more useful for future visitors. – moewe May 20 '18 at 14:04
• BTW: I really hope someone comes along and tells you how to do this with a more elegant method than a manual tabbing. The code is really hard to read and it looks like it would require quite a bit of manual intervention. – moewe May 20 '18 at 14:05
• A bit of feedback if the answers helped you would be appreciated. If one of the answers worked for you, upvoting and accepting is a nice gesture to say thank you. If for some reason the answer did not work for you, leave a comment and explain why/how it did not work for you. – moewe May 28 '18 at 7:15

For what it's worth here is a solution using tabular instead of tabbing. It doesn't look exactly like your screenshot, but I'd say it comes close, the input seems more natural to me and there is no need for manual spacing, which requires trial and error and makes the source hard to read.

countedtabular is a tabular that automatically numbers its rows; the heading/first row is not numbered. You can use the optional argument to continue counting up from a different counter (for example if you use gb4e's exe environment).

In the example below all columns have a fixed size of 1.3cm, but you could vary the size or use d/c for automatic sizing.

Because you used math mode, all columns are automatically set in math mode. If you decide you don't like that you can switch to the non-math-mode column specifiers.

The box is drawn with TikZ using the fit library (see §52 Fitting Library of the PGF manual). You simply mark up the points that you want to be inside the rectangular box and the rest happens more or less automatically. You get a better result if you use a node-\tikzmark (see https://tex.stackexchange.com/a/235662/35864). Instead of typing the text that you want to have inside the box directly into the cell like s/(n), you give it a clever name (cell-sn) wrap it in \tikztextmark: \tikztextmark{cell-sn}{s/(n)}. Then you simply name cell-sn in \drawboxaroundnodes and it will be included in the box.

\documentclass[british]{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{babel}

\usepackage{array}
% new column formats
% math mode built in
\newcolumntype{k}{>{$}l<{$}}
\newcolumntype{d}{>{$}c<{$}}
\newcolumntype{q}{>{$}r<{$}}
% fixed width
% see lockstep's answer in https://tex.stackexchange.com/a/12712/35864
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
% fixed width and math
\newcolumntype{K}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}$}p{#1}<{$}}
\newcolumntype{D}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}$}p{#1}<{$}}
\newcolumntype{Q}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}$}p{#1}<{$}}

\usepackage{etoolbox}

% this implements a table with numbered rows
% if the optional argument is specified the counter of that name
% is used to display the row numbers
\makeatletter
\newcounter{ctdtb@rowcount}
\newenvironment{countedtabular}[2][ctdtb@rowcount]
{\noindent
\setlength{\tabcolsep}{1em}%
\setcounter{ctdtb@rowcount}{-1}%
\begin{tabular}%
{@{}%
>{\refstepcounter{ctdtb@rowcount}%
\ifnumgreater{\value{ctdtb@rowcount}}{0}
{\ifstrequal{#1}{ctdtb@rowcount}
{}%                    only step the counter
{\refstepcounter{#1}}% if it is not the row counter
(\csuse{the#1})}
{}}R{22.77222pt}%<- hard-coded for gb4e, \labelwidth would work for normal enumerates
#2}}
{\end{tabular}}

\usepackage{tikz}
\usetikzlibrary{calc,fit}
\newif\ifstartedinmathmode
% https://tex.stackexchange.com/a/235662/35864
% https://tex.stackexchange.com/a/46496/35864
% set a node at position of text
% star: if star: no-autodetection of mode text typeset in text mode
%       no star:  surrounding text/math mode is detected and preserved
% first argument: internal name of the node
% second argument: text
% e.g.: \tikztextmark{nodefoo}{Hullo} typesets "Hullo"
% the position can then be called with nodefoo
\newcommand{\tikztextmark}{%
\@ifstar
{\startedinmathmodefalse}
{\relax
\ifmmode
\startedinmathmodetrue
\else
\startedinmathmodefalse
\fi}%
\tikztextmark@i}

\newcommand*{\tikztextmark@i}[2]{%
\tikz[overlay,remember picture,baseline]\node [anchor=base] (#1)
{\ifstartedinmathmode$#2$\else#2\fi};}

% automatically draw a box around the given coordinates
% optional argument to change the style and other settings
% mandatory argument takes a list of coordinates
\newcommand*{\drawboxaroundnodes}[2][]{%
\begin{tikzpicture}[overlay,remember picture]
\node[draw=black, thick, inner sep=0pt, fit=#2, #1] {};
\end{tikzpicture}%
}
\makeatother

\makeatletter
\newcounter{examples}
\usepackage{gb4e,enumitem}
\begin{document}
\begin{exe}
\ex Hallo
\ex Bye
\end{exe}

\begin{exe}
\ex Hallo
\ex Tschüss
\end{exe}

% compare and contrast the third and fourth row for different placement of the text
% compare and contrast the first and second row for the box anchors
\begin{countedtabular}[exx]{*{5}{D{1.3cm}}}
&\textit{Paul}&\textit{thinks}&\textit{that}&\textit{John}&\textit{sleeps}\\[12pt]
&n & s/(n)[n] & n/[s] & n & s/(n)\\
&n & s/(n)[n] & n/[s] & \multicolumn{2}{d}{s}\\
&n & s/(n)[n] & \multicolumn{3}{d}{n}\\
&\multicolumn{5}{d}{s.}\\
\end{countedtabular}

\begin{countedtabular}[exx]{*{5}{D{1.3cm}}}
&\textit{Paul}&\textit{thinks}&\textit{that}&\textit{John}&\textit{sleeps}\\[12pt]
&n & s/(n)[n] & n/[s] & \tikztextmark{cell-n}{n} & \tikztextmark{cell-sn}{s/(n)}\\
&n & s/(n)[n] & n/[s] & \multicolumn{2}{c}{\tikztextmark{cell-s}{s}}\\
&n & s/(n)[n] & \multicolumn{2}{d}{n}\\
&&\multicolumn{1}{q}{s.}\\
\end{countedtabular}
% draw the box: give all coordinates in round brackets
\drawboxaroundnodes{(cell-sn) (cell-s) (cell-n)}

\begin{exe}
\ex Hoi
\ex Doei
\end{exe}

\end{document}


TikZ is usually a good way to go to draw frames, here is the code I have used:

\documentclass[]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{booktabs}

\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1) {};}
\newcommand{\DrawBox}[3][]{%
\tikz[overlay,remember picture]{
\draw[black,#1]
($(#2)+(-0.5em,2.0ex)$) rectangle
($(#3)+(0.75em,-0.75ex)$);}
}

\begin{document}
\begin{tabbing}
\hspace{0.8em} \hspace{6em} \= \textit{Paul} \hspace{1.5em} \=
\textit{thinks} \hspace{2.8em} \= \textit{that} \hspace{2.5em} \=
\textit{John} \hspace{1.2em} \= \textit{sleeps}\\~\\
\hspace{0.8em}$(14)$     \hspace{5em} \= $n$ \hspace{2em} \= $s/(n)[n]$
\hspace{2em} \= $n/[s]$ \hspace{3em} \= \tikzmark{top left 1} $n$\hspace{2em} \= $s/(n)$\\
\hspace{0.8em}$(15)$     \hspace{5em} \> $n$ \hspace{2em} \> $s/(n)[n]$ \hspace{2em} \> $n/[s]$ \hspace{4.8em} \= $s$ \hspace{20pt}\tikzmark{bottom right 1} \\
\hspace{0.8em}$(16)$     \hspace{5em} \> $n$ \hspace{2em} \> $s/(n)[n]$
\hspace{5em} \= $n$\\
\hspace{0.8em}$(17)$ \hspace{5em} \> \> \= \hspace{2.5em} $s.$
\end{tabbing}

\DrawBox[thin, black]{top left 1}{bottom right 1}

\end{document}


The code I have used comes from this post. You can do more adjusting regarding the size of the frame, the color, etc... The idea is to position Tikzmarks in the table and then draw the frame around them as an overlay.

Hope it helps.

Romain