Typesetting a large matrix in LaTeX - latex

I have a 3x12 matrix I'd like to input into my LaTeX (with amsmath) document but LaTeX seems to choke when the matrix gets larger than 3x10:
\begin{equation}
\textbf{e} =
\begin{bmatrix}
1&1&1&1&0&0&0&0&-1&-1&-1&-1\\
1&-1&0&0&1&1&-1&-1&0&0&1&-1\\
0&0&1&-1&1&-1&1&-1&1&-1&0&0
\end{bmatrix}
\end{equation}
The error: Extra alignment tab has been changed to \cr. tells me that I have more & than the bmatrix environment can handle. Is there a proper way to handle this? It also seems that the alignment for 1's and the -1's are different, is that also expected of the bmatrix?

From the amsmath documentation (texdoc amsmath):
The amsmath package provides some
environments for matrices beyond the
basic array environment of LATEX. The
pmatrix, bmatrix, Bmatrix, vmatrix and
Vmatrix have (respectively) ( ), [
], { }, | |, and ∥
∥ delimiters built in. For naming
consistency there is a matrix
environment sans delimiters. This is
not entirely redundant with the array
environment; the matrix environments
all use more economical horizontal
spacing than the rather prodigal
spacing of the array environment.
Also, unlike the array environment,
you don’t have to give column
specifications for any of the matrix
environments; by default you can have
up to 10 centered columns. (If you
need left or right alignment in a
column or other special formats you
must resort to array.)
i.e. bmatrix defaults to a 10 column maximum.
A footnote adds
More precisely: The maximum number of
columns in a matrix is determined by
the counter MaxMatrixCols (normal
value = 10), which you can change if
necessary using LATEX’s \setcounter or
\addtocounter commands.

If you came to this page looking for the exact command (thanks to Scott Wales for the answer), you want this in your preamble:
\setcounter{MaxMatrixCols}{20}
Where you can replace 20 with the maximum number of columns you want.

The answer by Scott is correct, but I've since learned you can override the alignment. Taken from http://texblog.net/latex-archive/maths/matrix-align-left-right/
\makeatletter
\renewcommand*\env#matrix[1][c]{\hskip -\arraycolsep
\let\#ifnextchar\new#ifnextchar
\array{*\c#MaxMatrixCols #1}}
\makeatother
Now allows the command:
\begin{bmatrix}[r] ....
to have right-alignment!

Instead of a bmatrix you can use +bmatrix from the tabularray package:
\documentclass{article}
\usepackage{tabularray}
\UseTblrLibrary{amsmath}
\begin{document}
\begin{equation}
\textbf{e} =
\begin{+bmatrix}
1&1&1&1&0&0&0&0&-1&-1&-1&-1\\
1&-1&0&0&1&1&-1&-1&0&0&1&-1\\
0&0&1&-1&1&-1&1&-1&1&-1&0&0
\end{+bmatrix}
\end{equation}
\end{document}

Related

How to change () to [] in "lem" enviroment TeX?

I have a problem with brackets in enviroment "lem", which is defined in preamble. If you compile my MWE you get: "Lem 1 (Yoneda, 1996)", but I want: "Lem 1 [Yoneda, 1996]". How can I reach this?
I google about this problem, but I didn't find the answer.
\documentclass{article}
%\usepackage[T2A]{fontenc}
%\usepackage[cp1251]{inputenc}
%\usepackage[english]{babel}
\newtheorem{lem}{Lem}
\begin{document}
\begin{lem}[Yoneda, 1996]
Let $F$ be a functor from a locally small category ${\mathcal C}$ to
$\mathbf{Set}$. Then for each object $A$ of $\mathcal{C}$, the
natural transformations $\mathrm{Nat}(h_{A},F) = \mathrm{Hom}
(\mathrm{Hom}(A,-),F)$ from $h_{A}$ to $F$ are in one-to-one
correspondence with the elements of $F(A)$. That is,
$\mathrm{Nat}(h_{A},F) \cong F(A)$. Moreover, this isomorphism is
natural in $A$ and $F$ when both sides are regarded as functors from
${\mathcal{C}} \times \mathbf{Set}^{\mathcal {C}}$ to
$\mathbf{Set}$.
\end{lem}
\end{document}
There are a number of ways to achieve this. One primitive way is to update the internal command setting theorems for which an optional argument is provided - \#opargbegintheorem:
\documentclass{article}
\newtheorem{lem}{Lem}
\makeatletter
\renewcommand{\#opargbegintheorem}[3]{%
\trivlist
\item[\hskip\labelsep {\bfseries #1\ #2\ [#3]}]\itshape}% Changed (#3) to [#3]
\makeatother
\begin{document}
\begin{lem}[Yoneda, 1996]
Let\/~$F$ be a functor from a locally small category\/~$\mathcal{C}$ to\/~$\mathbf{Set}$. Then for each object\/~$A$ of\/~$\mathcal{C}$, the
natural transformations\/ $\mathrm{Nat}(h_A, F) = \mathrm{Hom}(\mathrm{Hom}(A, -), F)$ from\/~$h_A$ to\/~$F$ are in one-to-one
correspondence with the elements of\/~$F(A)$. That is,\/ $\mathrm{Nat}(h_A, F) \cong F(A)$. Moreover, this isomorphism is
natural in\/~$A$ and\/~$F$ when both sides are regarded as functors from\/~$\mathcal{C} \times \mathbf{Set}^{\mathcal{C}}$ to\/~$\mathbf{Set}$.
\end{lem}
\end{document}
This will, of course, have a global effect for all theorem-like structures defined via \newtheorem. However, such consistency is often a good thing.
A more adaptable change is shown here using amsthm (can also be used in conjunction with thmtools).

LaTex how to edit this equation?

I'm having issues turning this word equation into a LaTex equation. It's coming out looking dodgy, please help!
I added a screen shot of the equation I want, and what I end up getting when I copy and paste into LaTex:
WORD:
LATEX CODE:
\mathrm{=\ }\mathrm{C}_\mathrm{0}\mathrm{[1-}6(Dt)1/2aπ2-3Dta2] + 12(Dt)1/2an = 1∞exp(na(Dt)1/2)
and therefore nothing comes out and LaTex doesn't let me run it.
This is absolutely not a proper LaTeX equation code. I don't know what you know about LaTeX, but you cannot just copy and paste from Word or any software to you LaTex editor. Plus, you need to provide your full code for anyone being able to help you.
Anyway, running this MWE should work :
\documentclass[11pt, a4paper, twoside]{report}
% ===== PACKAGES DECLARATION =====
\usepackage{mathtools} % Replaces amsmaths + more features
\usepackage{amsfonts} % Maths fonts package
% ===== DOCUMENT BODY =====
\begin{document}
\begin{equation} % optional : use the "equation*" environment to remove equation number
% optional : use traditional math font by removing the \mathrm{} command
\mathrm{X = C_0 \left[ 1 - \frac{6(Dt)^{1/2}}{a \pi^2} - \frac{3Dt}{a^2}\right] + \frac{12(Dt)^{1/2}}{a} \sum_{n=1}^\infty \exp\left(\frac{na}{(Dt)^{1/2}} \right)}
% optional : remove auto-sized brackets by removing the \left and \right commands
\end{equation}
\end{document}
As written in the code, you may want to remove the equation number and the big auto-sized brackets (that are more readable in my opinion). Just remove the corresponding commands. Also, you should consider using the "normal" math font and not the roman one that is clearly different from the text and helps the reader to separate equations from inline small expressions you could insert in your document.
One first sketch:
\documentclass{article}
\begin{document}
\[
C_0\left[1-\frac{6(Dt)^{\frac{1}{2}}}{a\pi^2}-\frac{3Dt}{a^2}\right]+%
\frac{12(Dt)^{\frac{1}{2}}}{a}\sum^{\infty}_{n=1}%
\exp\left(\frac{na}{(Dt)^{\frac{1}{2}}}\right)
\]
\end{document}
No packages required. The output:
Than you can tune the math fonts and anything else.

Any way to keep R Markdown from putting a LaTeX environment in a new paragraph?

Environments like align and gather are pretty clearly designed for use within a paragraph of LaTeX text, as two line breaks between the document text and the start of the math environment inserts an egregious two paragraph's worth of vertical white space. Markdown, though, always starts any LaTeX environment two lines below the text that's above it, even if you begin the environment on the very same line of the markdown code/text, and even if you put 2 spaces before it in order to add a single line break. Since there's no multiline math dislay native to markdown, this poses a dilemma.
Running \vspace{-\baselineskip} before the environment compensates well enough, but of course it would be better to just tell markdown not to insert the line breaks in the first place. Is that possible? And if not, then what would be the easiest way to automatically run \vspace{-\baselineskip} before the beginning of each align (and/or align*, gather, gather*, etc.) environment?
MWE:
---
output:
pdf_document:
keep_tex: 1
---
The following environment will get marked up with an extra two lines between it and
this text, putting it on a new paragraph and creating a lot of whitespace above it,
whether or not there's any line breaks in the markdown code:
\begin{gather*}
A \\ B
\end{gather*}
This can of course be hackily corrected by subtracting vertical space:
\vspace{-\baselineskip} \begin{gather*}
A \\ B
\end{gather*}
The best you can do in this situation is to automatically insert \vspace{-\baselineskip} at the start of every specific environment using the etoolbox package:
---
output:
pdf_document:
keep_tex: 1
header-includes:
- \usepackage{etoolbox}
- \AtBeginEnvironment{gather}{\vspace{-\baselineskip}}
- \AtBeginEnvironment{gather*}{\vspace{-\baselineskip}}
---
The following environment will get marked up with an extra two lines between it and
this text, putting it on a new paragraph and creating a lot of whitespace above it,
whether or not there's any line breaks in the markdown code:
\begin{gather*}
A \\ B
\end{gather*}
This can of course be hackily corrected by subtracting vertical space:
\begin{gather*}
A \\ B
\end{gather*}
This, however, is not optimal, as the gap inserted by the environment depends on the amount of text ending the preceding paragraph. As a result of Pandoc's processing, the amount is always the same (\abovedisplayskip), so it may be "better" to use
header-includes:
- \usepackage{etoolbox}
- \AtBeginEnvironment{gather}{\vspace{\dimexpr-\baselineskip-\abovedisplayskip}}
- \AtBeginEnvironment{gather*}{\vspace{\dimexpr-\baselineskip-\abovedisplayskip}}
You'll have to do this for all amsmath-related display alignments.

Multiset notation in LaTeX

Does anyone know how to make (nice looking) double bracket multiset notation in LaTeX, i.e something like (\binom{n}{k}) where there are two outer brackets instead of 1 as in binomial? You can see an example of what I mean in http://en.wikipedia.org/wiki/Multiset under the heading "Multiset coefficients" with the double brackets.
In Wikipedia they typeset it as:
\left(\!\!{n\choose k}\!\!\right)
but although this works well for LaTeX in maths mode, with inline equations the outer bracket becomes much larger than the inner bracket.
I have also tried using
\genfrac{((}{))}{0pt}{}{n}{k}
but it has an error with the double brackets.
I am using \binom as well in my document, so I would like the bracket sizes to be similar for \binom and \multiset.
You can explicitly specify the size of the brackets via
\big( \Big( \bigg( or \Bigg(
Then use \! for negative space to get the brackets closer to each other.
One can use the e-TeX \middle command as follows:
\newcommand{\multibinom}[2]{
\left(\!\middle(\genfrac{}{}{0pt}{}{#1}{#2}\middle)\!\right)
}
This assumes that you are using the AMSmath package. If not, replace \genfrac with the appropriate construct using \atop.
(Of course this is a hack: the proper solution would be scalable glyphs for the doubled parenthesis, but I can't find any fonts that provide it.)
I'm surprised it wasn't googlable either, so I'll provide a solution here for posterity's sake.
It is also possible to define two different new commands, using \tbinom and \dbinom (section 4.11.2 of the User's Guide for the amsmath Package):
\documentclass{article}
\usepackage{amsmath}
\newcommand{\inlinebnm}[2]{\ensuremath{\big(\!\tbinom{#1}{#2}\!\big)}}
\newcommand{\displybnm}[2]{\bigg(\!\!\dbinom{#1}{#2}\!\!\bigg)}
\begin{document}
Text $\inlinebnm{a}{b}$ text. %% inline
Text \inlinebnm{a}{b} text. %% inline (also ok thanks to ensuremath)
\[
\displybnm{a}{b} %% display-style
\]
\end{document}

Adding a caption to an equation in LaTeX

Well, it seems simple enough, but I can't find a way to add a caption to an equation.
The caption is needed to explain the variables used in the equation, so some kind of table-like structure to keep it all aligned and pretty would be great.
The \caption command is restricted to floats: you will need to place the equation in a figure or table environment (or a new kind of floating environment). For example:
\begin{figure}
\[ E = m c^2 \]
\caption{A famous equation}
\end{figure}
The point of floats is that you let LaTeX determine their placement. If you want to equation to appear in a fixed position, don't use a float. The \captionof command of the caption package can be used to place a caption outside of a floating environment. It is used like this:
\[ E = m c^2 \]
\captionof{figure}{A famous equation}
This will also produce an entry for the \listoffigures, if your document has one.
To align parts of an equation, take a look at the eqnarray environment, or some of the environments of the amsmath package: align, gather, multiline,...
You may want to look at http://tug.ctan.org/tex-archive/macros/latex/contrib/float/ which allows you to define new floats using \newfloat
I say this because captions are usually applied to floats.
Straight ahead equations (those written with $ ... $, $$ ... $$, begin{equation}...) are in-line objects that do not support \caption.
This can be done using the following snippet just before \begin{document}
\usepackage{float}
\usepackage{aliascnt}
\newaliascnt{eqfloat}{equation}
\newfloat{eqfloat}{h}{eqflts}
\floatname{eqfloat}{Equation}
\newcommand*{\ORGeqfloat}{}
\let\ORGeqfloat\eqfloat
\def\eqfloat{%
\let\ORIGINALcaption\caption
\def\caption{%
\addtocounter{equation}{-1}%
\ORIGINALcaption
}%
\ORGeqfloat
}
and when adding an equation use something like
\begin{eqfloat}
\begin{equation}
f( x ) = ax + b
\label{eq:linear}
\end{equation}
\caption{Caption goes here}
\end{eqfloat}
As in this forum post by Gonzalo Medina, a third way may be:
\documentclass{article}
\usepackage{caption}
\DeclareCaptionType{equ}[][]
%\captionsetup[equ]{labelformat=empty}
\begin{document}
Some text
\begin{equ}[!ht]
\begin{equation}
a=b+c
\end{equation}
\caption{Caption of the equation}
\end{equ}
Some other text
\end{document}
More details of the commands used from package caption: here.
A screenshot of the output of the above code:

Resources