I would like to do x^(2n)
, however, when I try to do it, it shows up as (x^2) * n
, which is not what I want.
How do I do it the correct way?
math-mode
I would like to do x^(2n)
, however, when I try to do it, it shows up as (x^2) * n
, which is not what I want.
How do I do it the correct way?
There are a number of ways to address spacing issues. In the first example you give, reproduced in the first gather*
line in the code below, both the right large parenthesis and the exponent n
indeed seem to be spaced too loosely.
Inserting a "negative thinspace", \!
, before each of these two items (line 2) does eliminate the earlier looseness. Unfortunately, the overall expression now looks a bit cramped. In my view, the real culprits for making the expression in line 1 look unsatisfactory are (i) the near-collision between the opening (left) parenthesis and the subscript term below the product symbol and (ii) parentheses that are too tall relative to what they contain; see pp. 148-9 of the TeXbook for an explanation of this second point. In line 3 below, I therefore (a) insert a positive thinspace, \,
after the opening parenthesis and (b) use \biggl
and \biggr
parentheses, as they are slightly smaller (and also a tiny bit less wide!) than those produced by \left(
and \right)
. I think the result now looks both balanced and neither too loose nor too tight. :-)
The second example you provide is affected by the automatic additional spacing that's inserted when TeX creates \left
- and \right
parentheses. Switching to \biggl
and \biggr
parentheses, as in line 4 below, eliminates this excess space. If you do not wish to give up the convenience of automatic sizing of parentheses generated by \left
-\right
pairs, you could instead insert a negative thinspace, \!
, between the O
and the \left(
.
\documentclass[a4paper,11pt]{article}
\usepackage{amsmath}
\begin{document}
\noindent
\begin {gather*}
\intertext{Original}
\left( \prod_{i=1}^m X_i \right)^n \\ %% "line 1"
\intertext{less space on \emph{right}:}
\left( \prod_{i=1}^m X_i \!\right)^{\!n} \\ %% "line 2"
\intertext{more space on \emph{left}, biggl-biggr parens:}
\biggl(\,\prod_{i=1}^m X_i\biggr)^n \\ %% "line 3"
\intertext{Left-right vs.\ biggl-biggr parentheses}
O\left(\frac{1}{n}\right) \qquad O\biggl(\frac{1}{n}\biggr) %% "line 4"
\end{gather*}
\end{document}
This is a TikZ based solution working for math and text.
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.
A \fib{short} example with math $1 + 2^{\fib{2}} = \fib{5} = \sqrt{25}$.
beamer
classOne 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}
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}}$
\begin{equation}
1 + 3 = \fib{4} = \fib{\frac{8}{2}}
\end{equation}
\begin{equation}
(a + b)^2 = \fib{a^2 + 2ab + b^2}
\end{equation}
\begin{equation}
\begin{pmatrix}
1 \\ 2 \\ 3
\end{pmatrix}
\times
\begin{pmatrix}
4 \\ 5 \\ 6
\end{pmatrix}
=
\fib{\begin{pmatrix}
-3 \\ 6 \\ -3
\end{pmatrix}}
\end{equation}
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}} =
% \fib<4->{5} = \sqrt{25}$.
%\end{frame}
\end{document}
For the implementation we need tikz
(with the calc
library) and xparse
to implement the fill in boxes. I load mathtools
for the {pmatrix}
example …
\usepackage{xparse}
\usepackage{tikz}
\usetikzlibrary{calc}
\usepackage{mathtools}
Next step is to make @
available for internal command names.
\makeatletter
Then I define a new width, which will measure the width of the box, a factor to stretching the natural width (hand writing needs more space than printing) and an switch to show or hide the solutions (default: hide):
\newlength\fib@width
\def\fib@widthfactor{1.75}
\newif\iffibhideanswer
\fibhideanswertrue
Now I can define some TikZ styles. The every fill in box
defines some basics …
\tikzset{
every fill in box/.style={
inner xsep=0pt,
minimum height=3ex,
align=center,
font={\sffamily\slshape},
},
… and the next styles define various appearances for the fill in boxes.
...
The last style, is a dummy that can be used to change the style easily.
fill in/.style={
colored box,
},
}
Now I need a helper macro that either hides or shows the solution …
\NewDocumentCommand { \fib@hide } ...
… and one to print the box with the desired style:
\NewDocumentCommand { \fib@makebox } ...
Now I got everything to define \fib
(“fill in box”). It will have a starred version to always print the solution, an optional argument to change the style locally and the mandatory argument taking the content of the fill in box. Depending on the star (stored as bool in #1
) the solution hiding is set to false. If theres an optional argument (\IfValueT{#2}
) the style is redefined. Then it must be testest if the macro is in math mode (\ifmmode
= true) or in text mode (\ifmmode
= false). If the macro is inside math the current style must be checked and handled with \mathchoice
.
\NewDocumentCommand { \fib } ...
Last thing to do is to deactivate the @
:
\makeatother
Best Answer
You have to put the entire exponent in braces, treat it like the argument of any other LaTeX command. You can get away with (for example)
x^2
as a shortcut if you have only one character in your exponent. Otherwise, you need to put the whole thing in{ ... }
.So you need
x^{2n}
orx^{(2n)}
if you want the parentheses.Personally, I always put braces around my exponents, because I find it more readable (though others may see it as clutter) and because it's a good habit to get into. It's easy to forget the braces when you do need them otherwise, and it's much more of a pain correcting
x^n
tox^{2n}
than it is withx^{n}
. I admit, though, it's a bit more cumbersome.