LaTex how to edit this equation? - latex

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.

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 implement full LaTeX syntax in Rmarkdown?

I have a two part question regarding the use of LaTeX in Rmarkdown:
1) When working in Rmarkdown (with the intent to render to PDF), is there a rule for when we should just use the double dollar signs ($$) to write something in LaTeX or when we should use the LaTeX syntax to begin and end all of our LaTeX code (e.g. \documentclass{...}, \begin{document}, \end{document}, etc.
I believe I have read that it is okay to just use the latter option, and Rmarkdown will ignore all of the escaped latex commands if the document is rendered to anything other than PDF.
2) The reason I am asking, in this case, is that I am trying to incorporate some labelled matrix multiplication in an Rmarkdown document I am writing. Specifically, I would like to include some matrices that take the form show on this page. Here is the code:
\documentclass{article}
\usepackage{amsmath}
\newenvironment{spmatrix}[1]
{\def\mysubscript{#1}\mathop\bgroup\begin{pmatrix}}
{\end{pmatrix}\egroup_{\textstyle\mathstrut\mysubscript}}
\begin{document}
\begin{equation}
\begin{spmatrix}{A}
a & b \\
c & d
\end{spmatrix}
\begin{spmatrix}{x}
x_1 \\
x_2
\end{spmatrix}
=
\begin{spmatrix}{b}
b_1 \\
b_2
\end{spmatrix}
\end{equation}
\end{document}
How would one implement this code in Rmarkdown? Do you need to move the \usepackage call into the YAML as suggested in other threads discussing the loading of LaTeX packages in Rmarkdown? Is the first line, \documentclass{article} even needed within an Rmarkdown document?
I'm new to all of this, and thusfar, I've been able to get by using the double dollar signs to set off all my LaTeX code for simple equations and even a simple matrix here and there that I've tried to write, but for mathematical expressions that require more formatting, aligning, multi-line proofs, etc., most of the examples I've encountered are on the TEX boards written with syntax similar to the code above. I haven't been able to figure out how to implement these types of examples in Rmarkdown. Any helpful suggestions or pointers where to better understand this issue would be much appreciated!
It is indeed possible to put most body-level LaTeX constructs into the body of your Rmd file. Other backends will ignore these constructs, but the result might look change. So from my point of view your are binding yourself to PDF output. But that might be fine in your case.
Concerning your concrete problem:
amsmath is already included by the default template, which also takes care of \ḑocumentclass and the document environment.
You need to add the environment definition into a separate tex file (in my case preamble.tex) and include that file via the YAML headers.
You can then use the LaTeX constructs as is in the Rmd body.
Putting things together:
---
output:
pdf_document:
keep_tex: yes
includes:
in_header: preamble.tex
---
\begin{equation}
\begin{spmatrix}{A}
a & b \\
c & d
\end{spmatrix}
\begin{spmatrix}{x}
x_1 \\
x_2
\end{spmatrix}
=
\begin{spmatrix}{b}
b_1 \\
b_2
\end{spmatrix}
\end{equation}

Code in latex for braces

In LaTeX text, I sometimes need 'unpaired' parenthesis - ( without ) or vice versa; however, in such a case, delimiter check macro reports error. I found a simple solution: \symbol{40} and \symbol{41}, which aren't recognized as delimiters. The same works for brackets [ and ] (decimal codes are 91 and 93 respectively), but doesn't work for braces { and }. Their codes are 123 and 125 respectively, but \symbol{123} and \symbol{125} produce something like dash and quote. I use fontenc LCY, trying both text and math modes.
I've found a solution:\textbraceleft and \textbraceright, but why \symbol doesn't work in such a case?
Found it! This first code
\documentclass[border=5mm]{standalone}
\begin{document}
\symbol{123} and \symbol{125}
\end{document}
gives
as output, while
\documentclass[border=5mm]{standalone}
\usepackage[T1]{fontenc}
\begin{document}
\symbol{123} and \symbol{125}
\end{document}
gives this output instead:
Adding
\usepackage[T1]{fontenc}
provides the desired font encoding (fontenc package).
A (not-so-further) reading suggestion: font encoding vs. input encoding.

non-ASCII characters in Julia sourcefiles (e.g. θ) come out mojibake (e.g. Îÿ) using '\lstinputlisting{}' in LaTeX

I would like to include some Julia source in a LaTeX doc. I am familiar with the 'listings' package, and have crafted a decent 'lstdefinelanguage'. But I am having trouble with non-ASCII characters in the jl. I have played around with various 'lstset' and 'inputenc' options like 'extendedchars' and 'utf8' to no avail. I am using TeXShop on a Mac. Has anyone cracked this?
You can use literate option although it requires some set-up work. Here is a MWE:
\documentclass{article}
\usepackage{listings}
\usepackage{courier}
\lstset{basicstyle=\ttfamily}
\lstset{literate=
{δ}{{$\delta$}}1
{ϵ}{{$\epsilon$}}1
}
\begin{document}
\begin{lstlisting}
function f()
ϵϵϵϵϵ = 1
xxxxx = 3
δδδδδ = 2
ϵ + x + δ
end
\end{lstlisting}
\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