3

I am trying some samples. They make a frame around the text. I know there are already realized commands for it but I want to practice leaders, rules and boxes in LaTeX. So the examples below are kind of artificial.

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

\begin{document}
% defining new box types
\newsavebox{\textbox}
\newsavebox{\borderbox}
\sbox{\borderbox}{*}
\sbox{\textbox}{\parbox[b]{0.5\textwidth}{The origins of mathematical thought lie in the concepts of number, patterns in nature, magnitude, and form. Modern studies of animal cognition have shown that these concepts are not unique to humans. Such concepts would have been part of everyday life in hunter-gatherer societies. The idea of the "number" concept evolving gradually over time is supported by the existence of languages which preserve the distinction between "one", "two", and "many", but not of numbers larger than two.}}

\section{With leaders}
% to see the sizes of \textbox
height=\the\ht\textbox,\quad depth=\the\dp\textbox

% \totalht = \ht\textbox + \dp\textbox (height + depth)
\newlength{\totalht}
\setlength{\totalht}{\ht\textbox}
\addtolength{\totalht}{\dp\textbox}

% \totalwd = \wd\textbox + 2\wd\borderbox
\newlength{\totalwd}
\setlength{\totalwd}{\wd\textbox}
\addtolength{\totalwd}{2\wd\borderbox} % one \wd for the left asterisk(*) and one for the right

% managing the placement
\vbox{%
\hbox to \totalwd{\leaders\copy\borderbox\hfil}%
\hbox{%
\vbox to \totalht{\leaders\copy\borderbox\vfil}%
\copy\textbox%
\vbox to \totalht{\leaders\copy\borderbox\vfil}}%
\hbox to \totalwd{\leaders\copy\borderbox\hfil}}

\section{With rules}
\vbox{%
\hrule%
\hbox{%
\vrule%
\copy\textbox%
\vrule}%
\hrule}
\end{document}

The second sample works okay. It makes a framed text using rules. There is a question with the first example. If to compile the code above, it can be noticed that the vertical boxes of asterisks are kind of not completed. The last line of the text (starting from the word "numbers") needs an asterisk on the left and on the right. How to make it automatically? In my code, I tried to calculate the total height of the text box and make \vbox'es to have this height but somehow they are shorter. Moreover, if I increase \totalht, the problem still remains... What should I change in my code?

enter image description here

0

3 Answers 3

4

There are three problems in your code.

First: the \leaders primitive command puts its items without spaces between them, but the given width or height cannot be achieved exactly. You can use \xleaders instead \leaders, then the given width and height is achieved exactly, but there are little spaces between items (* in your example). You cannot require both properties at once.

Second: the \vboxes with frame have zero depth and they are placed to the baseline of the \textbox, no to the bottom of the \textbox. You have to move them lower by depth of the \textbox.

Third: boxes inside outer \vbox are positioned according to baseline, it means there are vertical spaces between them. These spaces can be removed by the \offinterlineskip macro.

Corrected code:

\newbox\borderbox
\newbox\textbox

\setbox\borderbox=\hbox{*}
\setbox\textbox=\vbox{\hsize=.5\hsize The origins of mathematical thought lie in the concepts of number, patterns in nature, magnitude, and form. Modern studies of animal cognition have shown that these concepts are not unique to humans. Such concepts would have been part of everyday life in hunter-gatherer societies. The idea of the "number" concept evolving gradually over time is supported by the existence of languages which preserve the distinction between "one", "two", and "many", but not of numbers larger than two.}

\def\totalwd{\dimexpr\wd\textbox+2\wd\borderbox\relax}
\def\totalht{\dimexpr\ht\textbox+\dp\textbox}

\vbox{\offinterlineskip
\hbox to \totalwd{\xleaders\copy\borderbox\hfil}%
\hbox{%
\lower\dp\textbox\vbox to \totalht{\xleaders\copy\borderbox\vfil}%
\copy\textbox%
\lower\dp\textbox\vbox to \totalht{\xleaders\copy\borderbox\vfil}}%
\hbox to \totalwd{\xleaders\copy\borderbox\hfil}}

\bye
4

You want to frame the text box inside an integral multiple of the width or height of the asterisk.

\documentclass[a4paper,12pt]{article}

% defining new box types
\newsavebox{\textbox}
\newsavebox{\borderbox}
\newlength{\textboxwd}
\newlength{\textboxht}

\sbox{\borderbox}{*}

\newcommand{\sometext}{The origins of mathematical thought lie in 
  the concepts of number, patterns in nature, magnitude, and form. 
  Modern studies of animal cognition have shown that these concepts
  are not unique to humans. Such concepts would have been part of
  everyday life in hunter-gatherer societies. The idea of the
  ``number'' concept evolving gradually over time is supported by
  the existence of languages which preserve the distinction between
  ``one'', ``two'', and ``many'', but not of numbers larger than two.}

\begin{document}

% set the box to the desired width, with the reference point at the bottom
\setbox\textbox=\vbox{\hsize=0.5\textwidth\sloppy\noindent\sometext\par\vspace{0pt}}
% round the height up to the nearest multiple of the height of \box\borderbox
\setlength{\textboxht}{\fpeval{ceil(\ht\textbox/(\ht\borderbox))}\ht\borderbox}
% round the width up to the nearest multiple of the width of \box\borderbox
\setlength{\textboxwd}{\fpeval{ceil(\wd\textbox/(\wd\borderbox))}\wd\borderbox}

% typeset
\hbox to \dimeval{\textboxwd+2\wd\borderbox}{\xleaders\copy\borderbox\hfill}
\nointerlineskip
\hbox{%
  \vbox to \textboxht{\xleaders\copy\borderbox\vfill}%
  \vbox to \textboxht{
    \vss
    \hbox to \textboxwd{\hss\copy\textbox\hss}
    \vss\vss
  }%
  \vbox to \textboxht{\xleaders\copy\borderbox\vfill}%
}
\nointerlineskip
\hbox to \dimeval{\textboxwd+2\wd\borderbox}{\xleaders\copy\borderbox\hfill}

\end{document}

enter image description here

2
  • 1
    The space between the top row of asterisks and the first asterisk below it on each side looks just a hair wider than the spaces between the asterisks that continue down the sides. Compare the bottom corners. (Otherwise, everything lines up nicely.) Nov 12, 2023 at 21:05
  • 2
    @barbarabeeton Usual eagle eye! I forgot a \nointerlineskip.
    – egreg
    Nov 12, 2023 at 21:16
2

Perhaps something like

enter image description here

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

\begin{document}
% defining new box types
\newsavebox{\textbox}
\newsavebox{\borderbox}
\sbox{\borderbox}{$\ast$}
\sbox{\textbox}{\parbox[b]{0.5\textwidth}{The origins of mathematical thought lie in the concepts of number, patterns in nature, magnitude, and form. Modern studies of animal cognition have shown that these concepts are not unique to humans. Such concepts would have been part of everyday life in hunter-gatherer societies. The idea of the "number" concept evolving gradually over time is supported by the existence of languages which preserve the distinction between "one", "two", and "many", but not of numbers larger than two.}}

\section{With leaders}
% to see the sizes of \textbox
height=\the\ht\textbox,\quad depth=\the\dp\textbox

% \totalht = \ht\textbox + \dp\textbox (height + depth)
\newlength{\totalht}
\setlength{\totalht}{\ht\textbox}
\addtolength{\totalht}{\dp\textbox}


% \totalwd = \wd\textbox + 2\wd\borderbox
\newlength{\totalwd}
\setlength{\totalwd}{\wd\textbox}
\addtolength{\totalwd}{2\wd\borderbox} % one \wd for the left asterisk(*) and one for the right

% managing the placement
\vbox{%
\hbox to \totalwd{\cleaders\copy\borderbox\hfil}%
\hbox{%
\vbox to \totalht{\leaders\copy\borderbox\vfil\kern-.7\baselineskip}%
\copy\textbox%
\vbox to \totalht{\leaders\copy\borderbox\vfil\kern-.7\baselineskip}}%
\hbox to \totalwd{\cleaders\copy\borderbox\hfil}}

\section{With rules}
\vbox{%
\hrule%
\hbox{%
\vrule%
\copy\textbox%
\vrule}%
\hrule}
\end{document}

You must log in to answer this question.

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