LaTeX: How to adjust how the Sum symbol turns out - latex

Not sure if LaTeX counts as programming, or if my question even makes sense, but I have this LaTeX expression (or what you call it):
\sum_{k=1}^n k^2 = 1+4+9+\ldots +n^2 =
\frac{1}{3}n^3 + \frac{1}{2}n^2 + \frac{1}{6}n
The problem is that the k=1 and n comes next to, i.e. after, the sum symbol, instead of above and below it. Is there a way I can change this? I have tried to show visually what I mean below. The sum symbol represented as Xs :
n
XXX XXX n
XXX vs XXX
XXX XXX k=1
k=1
I want the first kind, but am getting the second.

Try
\sum\limits_{k=1}^n k^2
if you want the sum limits to appear above and below the sum sign in an inline equation.

A more general solution to force a formula that is appearing in inline style to appear in display style is to start the formula with a \displaystyle declaration, e.g.
$\displaystyle \sum_{k=1}^n k^2$
This will work for any expression that appears differently in inline and display environments, such as \frac, \int, \lim, etc. You can also control the scope of the \displaystyle command by enclosing the desired expression in braces. For example, if you want the sum in your example formula to appear in display style but not the fractions, you could use
${\displaystyle \sum_{k=1}^n k^2} = 1+4+9+\ldots +n^2 =
\frac{1}{3}n^3 + \frac{1}{2}n^2 + \frac{1}{6}n$

I think you will need to use the equation environment for that:
\begin{equation}
\sum_{k=1}^n k^2 = 1+4+9+\ldots +n^2 = \frac{1}{3}n^3 + \frac{1}{2}n^2 + \frac{1}{6}n
\end{equation}

Related

Mixing text and equations in latex

I am working on some algorithm documenation for a project and trying to write out the equations in latex.
The one problem I am encountering and have not found a nice way (assuming there is one) is mixing text and equations in a single line.
Here is an example of what I am doing (and later how I am doing it).
I am defining the equation, and than what each variable means (left aligned text hence the &).
The latex code to generate this
\begin{equation}
A = 3B * 4C + 5D
\end{equation}
Where:
\begin{flalign*}
&A = Something \: cool\\
&B = Something \: cooler\\
&C = Something \: even \: cooler!!\\
\end{flalign*}
My questions are:
Is there a better way to do spaces in between words besides putting \: everywhere?
If I dont put the \: I get this below, all the words are combined?
Is this the most latex idiomatic way to acheive this? Am I missing something that could help me?
So I can get the output the way I want, I just want to make sure its "correct" before I get to deep.
You should never set whole words in math mode. Besides the obvious problem with spaces you noticed, this will also completely mess up the kerning between the letters.
Instead you can use the \text{...} macro from the amsmath package.
The amsmath package also provides the \intertext macro, which you could use to insert Where: while retaining alignment of the equal signs in the equations above and below:
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\begin{flalign}
A &= 3B \cdot 4C + 5D\\
\intertext{Where:}
A &= \text{Something cool}\notag\\
B &= \text{Something cooler}\notag\\
C &= \text{Something even cooler!!}\notag
\end{flalign}
\end{document}

How to simplify cos(pi)?

I'am new to Maxima and would like to use it for Denavit-Hartenberg matrices (consists of a lot of cos and sin terms). The problem is, that maxima does not simplify the following expression:
ex: x*cos(pi);
I expect, that Maxima simplifies ex to -x. How can this been done? (ratsimp(ex) and trigsimp(ex) have no effects)
In Maxima's dialect, the correct name of the constant is %pi. With it, it should simplify correctly.
As others have said, %pi is the correct name of the constant in Maxima. pi is simply rendered as π in GUIs like wxMaxima because all Greek letters are (you can have a variable named "π", which has nothing to do with the value of the constant π=3.14159...).
By the way, other predefined constants are written with the % character as well, such as for example
%e (=exp(1))
%i (=sqrt(-1))
%phi (the golden section)
The manual's index lists all % candidates.
Note that other useful constants that can not be expressed by digits, such as inf or false do not have the percent character.

Implicit linebreaks in LaTeX equations

I wonder if there is any way to invert the way the LaTeX interprets linebreaks in equations? E.g., I dont want to insert them explicitly like,
\begin{gather}
x = y \\
a = c
\end{gather}
, but implicitly like,
\begin{gather}
x = y
a = c
\end{gather}
Thanks.
This is against the intention of TeX’s author, who believed that math must be typeset by hand. I tried obeylines, but to no avail. I guess it’s possible by making new line active, but you should ask the cracks over at Stack Exchange, a branch of Stack Overflow for TeX and LaTeX.
The breqn package will automatically insert linebreaks in equations when the line is full. I don't know of anything that will do break as you ask. If it is a big deal you could use perltex to define a macro that would do it for you. I will try to mock one up as an example.

aligning equations in latex

I have the following code in an attempt to align things in latex using amsmath package:
\begin{alignat}{3}
\text{max} && c^Tx &=\\
\text{s.t.} && Ax &\leq b \\
&& x &\geq 0
\end{alignat}
Basically, i would like for max and s.t. to be in one column, c^Tx, Ax, x to be in second column, and lastly b and 0 to be in the last column. I'm not sure why it doesn't work (it clumps max and c^Tx together for some reason.
if anyone can help me out with this it would be much appreciated!
With math-mode \text{} you should put in some explicit whitespace such as \quad. But max smells like a log-like symbol so you should be using pre-defined \max or self-defined \operatorname{max} instead of \text{max}.
Additionally, the parameter for the alignat environment should be 2 in this case. The param is the number of alignment structures and can be calculated by solving n from a=2n-1 where a is the number of ampersands on a row. However, it doesn't seem to have a difference in this case.
I believe what's happening here is that max and s.t. are being right-aligned, which is running them right up next to c^Tx and Ax. If you just add some whitespace to the right-hand-side of max and s.t., you should be in business.
(Also, laalto is right, you should definitely use \operatorname{max} if max is some sort of operator or function. Though I'm not really sure what you're doing, so maybe it isn't.)

Can you iterate in LaTeX?

I'm new to LaTeX and I must say that I am really struggling with it. I discovered the \newcommand command that is kind of like a function/method in regular programming languages. You can give it arguments and everything.
I was wondering though, can I somehow iterate in LaTeX? Basically, what I would like to do is create a table with N+1 columns where the first row just contains a blank cell and then the numbers 1, 2, ..., N in the other columns. I only want to give N as an argument to this 'function' (newcommand).
Here is an example of something that might look like what I'm looking for (although obviously this won't work):
\newcommand{\mytable}[2]{
\begin{tabular}{l|*{#1}{c|}} % table with first argument+1 columns
for(int i = 1; i <= #1; i++) "& i" % 'output' numbers in different columns
\\\hline
letters & #2 % second argument should contain actual content for row
\\\hline
\end{tabular}
}
Call it with:
\mytable{3}{a & b & c}
Output should be:
| 1 | 2 | 3 |
--------+---+---+---+
letters | a | b | c |
--------+---+---+---+
Does anyone know if something like this is possible?
Thanks!
Just make the following into a new command and be sure to use package ifthen.
\begin{tabular}{l|*{10}{c|}}
\newcounter{count}
\whiledo{\value{count}<10}{
\ifthenelse{\value{count}=0}{}{\the\value{count}}
\ifthenelse{\value{count}<9}{&}{\\}
\stepcounter{count}
}
letters&a&b&c&d&e&f&g&h&i\\
\end{tabular}
Auntie Google says yes.
You can use the \loop or \repeat tokens. Or the multido package.
Sure it's possible. You can also recur. eplain has iteration macros in it, see, eg, here.
Another possibility (if you're lazy like me) is perltex

Resources