LaTeX - Break in math [closed] - math

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
The community reviewed whether to reopen this question 1 year ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I would like to be able to break lines in a math environment but in a way that keeps a symbol in front of the formula.
For example, the sum (sigma) followed by a long formula. So it goes out of the page but I would like to keep the formula next to the sigma even if there's a line break.
Here's a snippet so you can better reproduce it.
\documentclass{article}
\usepackage{amsmath,amsfonts,amssymb,mathtools}
% allows me to increase the font size of math blocks
\usepackage{graphicx} % \scalebox
\usepackage{environ}
\NewEnviron{mymath}{%
\[
\scalebox{1.3}{$\BODY$}
\]
}
\begin{document}
Lorem ipsum dolor sit amet
\begin{mymath}
\sum \limits _{\substack{
yyyyyyy,\\
zzzzzzzz,\\
www
}}
(
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
) + (
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
\end{mymath}
\end{document}
And here's a screenshot of what I'm trying to achieve.
So as you can see, I'm trying to keep the second parenthesis next to the sigma but under the first parenthesis.

First off: don't scale elements that contain text! If you don't like the size of your math block, use an appropriate font size instead.
You can use the split environment from amsmath :
\documentclass{article}
\usepackage{mathtools}
\begin{document}
Lorem ipsum dolor sit amet
\[
\begin{split}
\sum \limits_{\substack{
yyyyyyy,\\
zzzzzzzz,\\
www
}}
& (
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
) +\\
&(
xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
)
\end{split}
\]
\end{document}

Related

R Markdown Inline LaTeX Equations: `$` ... `$` vs `\(` ... `\)`

---
title: "esc"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
A. An amount between $5 and $10.
B. An amount between \$5 and \$10.
C. An amount between \\$5 and \\$10.
X. An equation $1 and 1 = 2$.
Y. An equation \$1 and 1 = 2\$.
Z. An equation \\$1 and 1 = 2\\$.
I've got a similar, but different question here. Please comment if I need to add clarification to differences. I'm asking two different questions and hope that's obvious.
Anyways, the output of knitting the .Rmd above is shown below. Why does example X properly invoke LaTeX, where example A does not? X and A seem almost identical to me, and I'm left wondering why they both don't transform whatever is inside the dollar signs $ into LaTeX text?
I'm aware I could use \( and \) to replace $ and $. I just want to know why $ and $ doesn't properly call LaTeX in my example. It seems arbitray that it works for X, but not A, although I doubt it's arbitrary.
Pandoc's manual, under the Pandoc's Markdown > Math section, probably explains it best:
Anything between two $ characters will be treated as TeX math. The opening $ must have a non-space character immediately to its right, while the closing $ must have a non-space character immediately to its left, and must not be followed immediately by a digit. Thus, $20,000 and $30,000 won’t parse as math. If for some reason you need to enclose text in literal $ characters, backslash-escape them and they won’t be treated as math delimiters.

Markdown PDF Knitr - MINUS SIGN (U+2212) error

When knitting my R markdown file to pdf the following error message occurs:
! Package inputenc Error: Unicode character − (U+2212)
(inputenc) not set up for use with LaTeX.
markdown
I know it has to do with the MINUS sign I'm using in some formulas, but I can't solve the problem.
I have already set the Typset LaTeX into pdf.
The formulas in question are:
\hat{\beta_1} = \frac{\sum_{i=1}^{n} (x_i - \bar{x})(y_i - \bar{y})}{\sum_{i=1}^{n} (x_i - \bar{x})^2}
and
\hat{\beta_0}=\bar{y} - \hat{\beta_1}\bar{x}
Another method to get around this error is to search and replace the offending unicode character, which can result from copy/pasting code or text into R.
For your situation, the offending symbol: − (en dash) should be replaced with - (hyphen).
Your latex can't process the en dash. It's subtle, but these characters have different widths. There are three types of horizontal punctuation “lines”:
Hyphen (-), used to hyphenate compound words and simple compound adjective (hence its name) and often used as the minus sign (-) in math
En dash (–), used to mark ranges in numbers, dates, scores and for complex compound adjectives
Em dash (—), used to separate extra information or mark a break in a sentence and other niche writing situations
This solves the problem.
title: "Your Title here"
output:
pdf_document:
latex_engine: xelatex
A little late but for anyone else who has the same problem:
Use \- instead of -

How do I print lines in a file only where second and second last character are the same? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I want to print all the lines of a file in which second and second last character are the same, such as with the following file (comments to the right are for explanatory purposes, they do not exist in the file):
hello james # second/second-last are 'ee' - match
how are you? # 'ou'
are you okay? # 'ry'
Is it past # 'ss' - match
Then the output should be
hello james
Is it past
How would I go about doing this?
You can use grep with grouping and backreference for this, e.g.:
grep -x ".\(.\).*\1." f1.txt
This pattern looks in given order for:
any character: .
another arbitrary character in a capture group: \(.\)
any number (including 0) of characters: .*
the same character previously captured (the backreference): \1
finally, the last arbitrary character: .
-x means it has to match the whole line rather than just some portion of it (same as using --line-regexp). As a result only the matched lines will be printed.
Here is an awk that compare second first and second last character:
awk '{b=split($0,a,"")} a[2]==a[b-1]' file
hello james
Is it past
If there are spaces or tabs at the end of the line, it can be trimmed away like this:
awk '{$1=$1;b=split($0,a,"")} a[2]==a[b-1]'
hello james
Is it past

Unix Replace specific column with one value for another string [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I would like to change the fourth column of a file when I have the following description for it. I can not find how to solve it. When the string 1-8 appears I want to replace it with 01-08 but only in the 4 column separated by pipe.
SBMM01|CAM|22|01-08|NAP|VL|OPEN|1
CCSM01|CAM||1-8|NAP|CR|CLOSED|1
EZEM01|CAM|19|01-08|SPL|CC|OPEN|5
SPTD01|CAM|29|25-32|CDO|VG|OPEN|1
NRFL01|||1-8|NAP|CR|CLOSED|5
|||1-8|NAP|CR|CLOSED|5
by
SBMM01|CAM|22|01-08|NAP|VL|OPEN|1
CCSM01|CAM||01-08|NAP|CR|CLOSED|1
EZEM01|CAM|19|01-08|SPL|CC|OPEN|5
SPTD01|CAM|29|25-32|CDO|VG|OPEN|1
NRFL01|||01-08|NAP|CR|CLOSED|5
|||01-08|NAP|CR|CLOSED|5
How can change it with sed or awk in Unix?
to change only the specified value
$ awk 'BEGIN{FS=OFS="|"} $4=="1-8"{$4="01-08"}1' file
you can more generally format printing digits zero padding as well.
With sed
sed -E 's/((.*|){3})(1-8)(.*)/\101-08\4/' infile

Which function should I use to read unstructured text file into R? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
This is my first ever question here and I'm new to R, trying to figure out my first step in how to do data processing, please keep it easy : )
I'm wondering what would be the best function and a useful data structure in R to load unstructured text data for further processing. For example, let's say I have a book stored as a text file, with no new line characters in it.
Is it a good idea to use read.delim() and store the data in a list? Or is a character vector better, and how would I define it?
Thank you in advance.
PN
P.S. If I use "." as my delimeter, it would treat things like "Mr." as a separate sentence. While this is just an example and I'm not concerned about this flaw, just for educational purposes, I'd still be curious how you'd go around this problem.
read.delim reads in data in table format (with rows and columns, as in Excel). It is not very useful for reading a string of text.
To read text from a text file into R you can use readLines(). readLines() creates a character vector with as many elements as lines of text. A line, for this kind of software, is any string of text that ends with a newline. (Read about newline on Wikipedia.) When you write text, you enter your system specific newline character(s) by pressing Return. In effect, a line of text is not defined by the width of your software window, but can run over many visual rows. In effect, a line of text is what in a book would be a a paragraph. So readLines() splits your text at the paragraphs:
> readLines("/path/to/tom_sawyer.txt")
[1] "\"TOM!\""
[2] "No answer."
[3] "\"TOM!\""
[4] "No answer."
[5] "\"What's gone with that boy, I wonder? You TOM!\""
[6] "No answer."
[7] "The old lady pulled her spectacles down and looked over them about the room; then she put them up and looked out under them. She seldom or never looked through them for so small a thing as a boy; they were her state pair, the pride of her heart, and were built for \"style,\" not service—she could have seen through a pair of stove-lids just as well. She looked perplexed for a moment, and then said, not fiercely, but still loud enough for the furniture to hear:"
[8] "\"Well, I lay if I get hold of you I'll—\"
Note that you can scroll long text to the left here in Stackoverflow. That seventh line is longer than this column is wide.
As you can see, readLines() read that long seventh paragraph as one line. And, as you can also see, readLines() added a backslash in front of each quotation mark. Since R holds the individual lines in quotation marks, it needs to distinguish these from those that are part of the original text. Therefore, it "escapes" the original quotation marks. Read about escaping on Wikipedia.
readLines() may output a warning that an "incomplete final line" was found in your file. This only means that there was no newline after the last line. You can suppress this warning with readLines(..., warn = FALSE), but you don't have to, it is not an error, and supressing the warning will do nothing but supress the warning message.
If you don't want to just output your text to the R console but process it further, create an object that holds the output of readLines():
mytext <- readLines("textfile.txt")
Besides readLines(), you can also use scan(), readBin() and other functions to read text from files. Look at the manual by entering ?scan etc. Look at ?connections to learn about many different methods to read files into R.
I would strongly advise you to write your text in a .txt-file in a text editor like Vim, Notepad, TextWrangler etc., and not compose it in a word processor like MS Word. Word files contain more than the text you see on screen or printed, and those will be read by R. You can try and see what you get, but for good results you should either save your file as a .txt-file from Word or compose it in a text editor.
You can also copy-paste your text from a text file open in any other software to R or compose your text in the R console:
myothertext <- c("What did you do?
+ I wrote some text.
+ Ah, interesting.")
> myothertext
[1] "What did you do?\nI wrote some text.\nAh, interesting."
Note how entering Return does not cause R to execute the command before I closed the string with "). R just replies with +, telling me that I can continue to edit. I did not type in those plusses. Try it. Note also that now the newlines are part of your string of text. (I'm on a Mac, so my newline is \n.)
If you input your text manually, I would load the whole text as one string into a vector:
x <- c("The text of your book.")
You could load different chapters into different elements of this vector:
y <- c("Chapter 1", "Chapter 2")
For better reference, you can name the elements:
z <- c(ch1 = "This is the text of the first chapter. It is not long! Why was the author so lazy?", ch2 = "This is the text of the second chapter. It is even shorter.")
Now you can split the elements of any of these vectors:
sentences <- strsplit(z, "[.!?] *")
Enter ?strsplit to read the manual for this function and learn about the attributes it takes. The second attribute takes a regular expression. In this case I told strsplit to split the elements of the vector at any of the three punctuation marks followed by an optional space (if you don't define a space here, the resulting "sentences" will be preceded by a space).
sentences now contains:
> sentences
$ch1
[1] "This is the text of the first chapter" "It is not long"
[3] "Why was the author so lazy"
$ch2
[1] "This is the text of the second chapter" "It is even shorter"
You can access the individual sentences by indexing:
> sentences$ch1[2]
[3] "It is not long"
R will be unable to know that it should not split after "Mr.". You must define exceptions in your regular expression. Explaining this is beyond the scope of this question.
How you would tell R how to recognize subjects or objects, I have no idea.

Resources