3

Is that possible to create a box (without frame) so that, even though its content is pretty big, it only occupies a small space in the text follow. For example, a tikz picture, however large it is, only takes a size of 1cm x 1cm if you put it into a text flow.


Update: To clarify the confusions, I meant to draw a box, visually large, but only occupy a small space on the text flow. For example, like the star in the picture below:

enter image description here

5
  • Just to be clear (based on the confusion in the answers): do you want to shrink a large box into a small one, or do you want to create a "virtual" small box with content of arbitrarily large visual size but only a fixed typographic size on the page?
    – Ryan Reich
    Oct 27, 2013 at 5:55
  • Also, with tikz there are better ways to make it not take up space. See the overlay key.
    – Ryan Reich
    Oct 27, 2013 at 5:56
  • @RyanReich: The latter one.
    – xzhu
    Oct 27, 2013 at 6:05
  • @RyanReich - Could you elaborate on what you mean by "a virtual small box with content of arbitrarily large visual size"?
    – Mico
    Oct 27, 2013 at 6:35
  • @Mico I meant a "virtual bounding box", a container that takes up some made-up amount of physical space but whose "ink" can be anything, even outside the box.
    – Ryan Reich
    Oct 27, 2013 at 14:51

4 Answers 4

4

You can use \raisebox to set the height (and depth) of the box and minipage to set its width. For example,

\raisebox{0pt}[1cm][0pt]{%
  \begin{minipage}{1cm}%
    % Your content goes here.
  \end{minipage}%
}

will make a box that rises 1cm above the baseline and is 1cm wide. For more information, see this page.

4
  • Did you mean to write about the command \resizebox?
    – Mico
    Oct 27, 2013 at 5:40
  • @Mico \resizebox stretches its contents to fit the new size; whereas \raisebox and minipage use their own dimensions without modifying the contents (assuming the contents of the minipage is one box), so that the contents are larger than the box reserved for them. Of course, it's not entirely clear whether trVoldemort wants the contents resized or not.
    – ChrisS
    Oct 27, 2013 at 5:47
  • 1
    This seems to be the LaTeX version of the plain TeX solution \hbox to 1cm{\hss\vbox to 1cm{\vss <content>\vss}\hss}, with the bonus of likely not having forgotten some technical detail.
    – Ryan Reich
    Oct 27, 2013 at 5:54
  • I think this is exactly what the OP is asking for. You may want to add a complete MWE with an image showing the result.
    – Daniel
    Oct 27, 2013 at 7:38
3

As Ryan Reich points out you can use the overlay key to make tikz ignore the path/picture contents.

From there on you have a number of choices which do not have to involve tikz. But here are two 'tikz-based' ideas:

Firstly, you can use the trim left and trim right keys to set the width (not sure which version of PGF these keys came in) and the baseline key to vertically align the picture.

However, much greater control can be obtained using the \useasboundingbox command in the tikzpicture, which I've tidied up in a picture corners key:

\documentclass[border=0.25in]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric}

\tikzset{%
    star in text/.style={
        shape=star,
        draw,
        minimum size=1cm,
        star point ratio=2,
    },
    picture width/.style={
        trim left=-#1/2, 
        trim right=#1/2,
    },
    picture corners/.style args={#1 and #2}{
        execute at end picture={
            \useasboundingbox #1 rectangle #2;
        }
    }
}
\begin{document}


\begin{minipage}{0.5in}
\centering

ab\tikz[picture width=1ex, baseline=-0.5ex]\node[overlay, star in text]{};cd

\vskip0.5in

ab\tikz[picture corners={(-0.5ex, -0.5ex) and ++(1ex, 1ex)}]\node[overlay, star in text]{};cd


\vskip0.5in

ab\tikz[picture corners={[draw] (-0.5cm, -0.5cm) and ++(0.5cm, 0.5cm)}]\node[overlay, star in text]{};cd

\vskip0.5in

ab\tikz[picture corners={[draw] (-0.5cm, 0cm) and ++(0.5cm, 0.5cm)}]\node[overlay, star in text]{};cd

\vskip0.5in

ab\tikz[picture corners={[draw] (0cm, 0cm) and ++(0.5cm, 0.5cm)}]\node[overlay, star in text]{};cd

\vskip0.5in

ab\tikz[picture corners={[draw] (0cm, -0.5cm) and ++(0.5cm, 0.5cm)}]\node[overlay, star in text]{};cd

\end{minipage}
\end{document}

enter image description here

2

The command \resizebox of the graphicx package can "resize" existing boxes into new boxes of specified horizontal and vertical lengths. To leave one of the resized lengths unspecified, supply ! as the parameter.

The following example shows how a "large box" (a black rectangle measuring 4cm wide and 3cm tall) can be "fit" into a new, smaller box of width set at 1cm. Of course, in practice you would replace \bigbox with a real diagram, graphic, table, etc.

enter image description here

\documentclass{article}
\usepackage{graphicx}
\newcommand{\bigbox}{\rule{4cm}{3cm}} % just a big black rectangle
\begin{document}
The small box containing the big box: \resizebox{1cm}{!}{\bigbox}

For reference, the big box: \bigbox
\end{document}
0

The \useanchorwidth parameter of the stackengine package is intended for what you wish, though one has a few complications to overcome. In the first image, I just set a large star inline. In the second image, I use the \stackon macro of stackengine (with a 0pt baseline shift on a long stack) to overlay the star over a space. By default, the stack width is the total width, in this case the star width. So in the third attempt, I set \useanchorwidth true, and then the star width becomes the width of the space only. The problem is that since the star is set after the "ab", it overwrites it.

In the final result, I reverse the order of the stack, that is, I stack ab~cd atop the star, so it does not overwrite the "ab"; however, the overall width is that of the star (the anchor), so I have to restack that atop a blank rule the width of ab~cd in order to achieve the right spacing.

\documentclass{article}
\usepackage{MnSymbol}
\usepackage{graphicx}
\usepackage{stackengine}
\usepackage{xcolor}
\usepackage{calc}
\def\stacktype{L}
\def\bigstar{\scalebox{3.5}{\raisebox{-0.3ex}{\color{yellow}$\largestar$}}}
\begin{document}
Inline: ab\bigstar cd

Stackon (w/ width): ab\stackon[0pt]{~}{\bigstar}cd

\renewcommand\useanchorwidth{T}
stackon (useanchorwidth): ab\stackon[0pt]{~}{\bigstar}cd

double stackon (useanchorwidth): \stackon[0pt]{\rule{\widthof{ab~cd}}{0ex}}{%
  \stackon[0pt]{\bigstar}{ab~cd}}

\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 .