# Graphical placeholder for “variables”

## Question

The image below exactly shows what I am trying to accomplish in LaTeX.

### Research

• Using \fbox (as Peter Grill suggested in this post):

\documentclass[]{article}
\usepackage{amsmath}

\begin{document}
\newcommand*{\Y}{\textbf{\fbox{?}}}%

$3 + \Y = 7$

\end{document}


I thought of replacing the question mark by a space and then removing the top border, but I have not found any material on how to hide specific borders.

• Using \fcolorbox:

Again, I do not know how to hide the top border.

• Using mdframed:

This does not work in math mode as far as I know.

## Small improvement to Donut E. Knot's post (former accepted answer)

Donut E. Knot's post perfectly answered the question, however, I want to provide a small improvement which is too small for an own answer, but still too useful for being lost in a comment.

Add the following code under Knot's code:

\newdimen{\phWidth}%
\newcommand\phAutoWidth[1]{%
\setlength{\phWidth}{\widthof{#1}}%
\ph[\phWidth]{#1}}


You now no longer need to explictly specify a width, instead, it will be automatically computed.

• \rule can do the job. – kiss my armpit Nov 30 '13 at 12:51
• You may like to have a look at Why is $…$ preferable to $$…$$? ;-) – Tobi Nov 30 '13 at 16:12
• @Tobi Thanks for the link! I updated my question. One question though: does the same apply for single dollar signs ($...$)? – ComFreek Dec 1 '13 at 18:50
• Hm … the problem with single $’s is not that big, while $$…$$ is the LaTeX syntax, $ … $ should render the same (which not applies for the -$$ style – as you know) … see tex.stackexchange.com/q/510/4918 – Tobi Dec 2 '13 at 0:06 ## 3 Answers This is a TikZ based solution working for math and text. ## Usage Use \fib{<text/math>} to create a fill in box. Use the starred version to show the solution of a single box or \fibhideanswerfalse to show all solutions. With \tikzset{fill in/.style={<box style>}} or \tikzset{<underline/bracket> style/.style={<drawing style>}} the style can be changed globally or with \fib[underlined box]{<text/math> for a single box. ## Examples A \fib{short} example with math$1 + 2^{\fib{2}} = \fib{5} = \sqrt{25}$.  ## beamer class One can use the following to have one slide with the empty boxes and the next one with the solutions on it. \begin{frame}{Beamer example} \only<2->{\fibhideanswerfalse} A \fib{short} example with math$1 + 2^{\fib{2}} = \fib{5} = \sqrt{25}$. \end{frame}  Or use the following to show the answers step by step. \begin{frame}{Beamer example} A \fib<2->{short} example with math$1 + 2^{\fib<3->{2}} = \fib<4->{5} = \sqrt{25}. \end{frame}  ## Code There’s also a german explanation in my blog: TeX-Beispiel der Monats: Lückentexte. \documentclass[fleqn]{article} %\documentclass{beamer} \usepackage{xparse} \usepackage{tikz} \usetikzlibrary{calc} \usepackage{mathtools} \makeatletter \newlength\fib@width \def\fib@widthfactor{1.75} \newif\iffibhideanswer \fibhideanswertrue \tikzset{ every fill in box/.style={ inner xsep=0pt, minimum height=3ex, align=center, font={\sffamily\slshape}, }, colored box/.style={ every fill in box, fill=yellow!50!white, }, framed box/.style={ every fill in box, draw, }, underline style/.style={}, underlined box/.style={ every fill in box, append after command={% \pgfextra{\begin{pgfinterruptpath} \draw [underline style] (\tikzlastnode.south west) -- (\tikzlastnode.south east); \end{pgfinterruptpath}} }, }, bracket style/.style={}, underbracked box/.style={ every fill in box, append after command={% \pgfextra{\begin{pgfinterruptpath} \draw [bracket style] ((\tikzlastnode.south west)+(0,2pt)$) |- (\tikzlastnode.south) -| ($(\tikzlastnode.south east)+(0,2pt)$); \end{pgfinterruptpath}} }, }, underoverbracked box/.style={ every fill in box, append after command={% \pgfextra{\begin{pgfinterruptpath} \draw [bracket style] ($(\tikzlastnode.north west)-(0,2pt)$) |- (\tikzlastnode.north) -| ($(\tikzlastnode.north east)-(0,2pt)$); \draw [bracket style] ($(\tikzlastnode.south west)+(0,2pt)$) |- (\tikzlastnode.south) -| ($(\tikzlastnode.south east)+(0,2pt)$); \end{pgfinterruptpath}} }, }, fill in/.style={ colored box, }, } \NewDocumentCommand { \fib@hide } { m } {% \iffibhideanswer \phantom{#1}% \else #1% \fi } \NewDocumentCommand { \fib@makebox }{ m }{% \settowidth{\fib@width}{\tikz\node[fill in]{#1};}% \begin{tikzpicture}[baseline=(fill in node.base)] \node (fill in node) [text width=\fib@widthfactor*\fib@width,fill in] {% \fib@hide{#1}% }; \end{tikzpicture}% } \NewDocumentCommand { \fib } { s d{<}{>} o m }{{% \IfBooleanT{#1}{\fibhideanswerfalse}% \IfValueT{#2}{\only<#2>{\fibhideanswerfalse}}% \IfValueT{#3}{\tikzset{fill in/.style={#3}}}% \ifmmode \mathchoice {\fib@makebox{$\displaystyle#4$}} {\fib@makebox{$\textstyle#4$}} {\fib@makebox{$\scriptstyle#4$}} {\fib@makebox{$\scriptscriptstyle#4$}} \else \fib@makebox{#4}% \fi \IfValueT{#2}{}% }} \makeatother \begin{document} % STYLE SETTING EXAMPLES %\tikzset{colored box/.append style={fill=black!15}} %\tikzset{fill in/.style={framed box}} %\tikzset{fill in/.style={underlined box}} %\tikzset{underline style/.style={densely dotted,thick}} %\tikzset{fill in/.style={underbracked box}} %\tikzset{fill in/.style={underoverbracked box}} %\tikzset{bracket style/.style={gray,thick}} %\fibhideanswerfalse % ARTICLE/BOOK EXAMPLES A \fib{short} example with math$1 + 2^{\fib{2}} = \fib{5} = \sqrt{25}$. \vspace{2cm} In \fib{text} mode and math$1 + 3 = \fib{4} = \fib{\frac{8}{2}}$$$1 + 3 = \fib{4} = \fib{\frac{8}{2}}$$ $$(a + b)^2 = \fib{a^2 + 2ab + b^2}$$ $$\begin{pmatrix} 1 \\ 2 \\ 3 \end{pmatrix} \times \begin{pmatrix} 4 \\ 5 \\ 6 \end{pmatrix} = \fib{\begin{pmatrix} -3 \\ 6 \\ -3 \end{pmatrix}}$$ With an asterisk, i.e. \verb+\fib*+, the \fib*{solution} is always visible. The optional argument can be used to change \fib*[underlined box]{styles} locally. % BEAMER EXAMPLES %\begin{frame}{Beamer example 1} % \only<2->{\fibhideanswerfalse} % A \fib{short} example with math$1 + 2^{\fib{2}} = \fib{5} =
%   \sqrt{25}$. %\end{frame} %\begin{frame}{Beamer example 2} % A \fib<2->{short} example with math$1 + 2^{\fib<3->{2}} =
in encoding T1.

\end{document}


• +1 thanks for your research. It's quite interesting that OT1 doesn't use a normal space while T1 does. – ComFreek Dec 1 '13 at 19:24