How to make all numbers and letter take up the same amount of space in latex? - latex

I'm trying to show step by step how to sort an array. But since the numbers and letters take up different amounts of space, the columns get staggered. I'm very new to latex and am not sure if there's an easy fix for this.
If that's not possible, is there some kind of grid system I can use to keep them in straight lines? (without showing the grid).

You can place each of the elements in a box that are of similar width and height. This will ensure that each take up the same amount of space. It's possible using eqparbox's \eqmakebox[<tag>][<align>]{<stuff>}. All \eqmakeboxes with the same <tag> are put in a box of maximum width of <stuff>. You can adjust the <align>ment of every element, if needed (centred, left-aligned or right-aligned).
\documentclass{amsart}
\usepackage{eqparbox}
\newcommand{\swl}[2][nmbr]{\eqmakebox[#1]{\strut #2}}
\begin{document}
\begin{tabular}{c}
\swl{a}\swl{b}\swl{m}\swl{0} \\
\swl{i}\swl{i}\swl{1}\swl{n}
\end{tabular}
\end{document}
The above code provides a wrapper \swl[<tag>]{<stuff>} that provides a similar-width letter macro. If you want a blank space, you can use \swl{~}.
eqparbox uses TeX's \label-\ref system to store the widths and therefore requires at least two compilations for any change (increase in width) in elements under the same <tag>.

Related

What is the best way to add new lines and new paragraphs in LaTeX?

I'm currently writing a report where I write new paragraphs like this:
I like sharks, sharks are cool.
Also bears are really cool.
Which basically has one completely empty line between then. In order to create a new paragraph I use the "\" code:
I like sharks, sharks are cool. \\
Also bears are really cool.
However this generates "Underfull \hbox (badness 10000)" error. What is the correct way of starting a new paragraph?
use \par after the line of code you want there to be a new paragraph
after
I like sharks, sharks are cool. \par
Also bears are really cool.
https://www.sharelatex.com/learn/Paragraphs_and_new_lines
If your looking for line breaks go here
https://www.sharelatex.com/learn/Line_breaks_and_blank_spaces
You have several options
\\ (two backslashes)
\newline
\hfill \break
\vspace{5mm}
Inserts a vertical spaces whose length is 5mm. Other LATEX units can be used with this command.
\vfill
Inserts a blank space that will stretch accordingly to fill the vertical space available. That's why the line "Text at the bottom of the page." is moved to the bottom, and the rest of the space is filled in.
\smallskip
Adds a 3pt space plus or minus 1pt depending on other factors (document type, available space, etc)
\medskip
Adds a 6pt space plus or minus 2pt depending on other factors (document type, available space, etc)
\bigskip
Adds a 12pt space plus or minus 4pt depending on other factors (document type, available space, etc)
Not sure if it's best practice, but I would use:
I like sharks, sharks are cool. \newline
Also bears are really cool.
\newline followed by an empty line will give you the space between the two lines.
I just put a blank line in between.
I like sharks, sharks are cool.
Also bears are really cool.
I wouldn't worry about an underfull \hbox warning though. You probably have better things to do than try to make all of LaTeX's warnings go away.
I would also take a look at \paragraph{} command, that may provide paragraph heading too.

get height on a block of latex output

I am trying to determine how to get the height on a block of latex output (not the whole document, and not the code..but rather a block of output). As an example of what I am trying to accomplish: i were to have the latex code
$\sum_{i=0}^\infty \frac{1}{n}>\infty$ \newline hello world \newline hello universe
The height of the above block of text is dependent on a number of things-font, margin size, and of course what the text is, as changing any of these parameters changes how many inches that output would be, but with default formatting its output would be something like 2 inches high.
I am hoping there is a package that does this!
Usually, the trick is to put whatever you want to measure into a box and then simply not typeset the box, but measure it:
\newdimen\height
\setbox0=\hbox{\Huge Hello, World!}
\height=\ht0 \advance\height by \dp0
The height is: \the\height
I think this will work:
\newlength{\somenamehere}
\settoheight{\somenamehere}{\hbox{...}}
Where ... is your content you like to measure. And you can then use \somenamehere as the height of that content.
Example:
\documentclass[english]{article}
\usepackage[T1]{fontenc}
\usepackage[latin9]{inputenc}
\usepackage{babel}
\begin{document}
\newlength{\heightofhw}
\settoheight{\heightofhw}{\hbox{Hello World!}}
Value = \the\heightofhw
\end{document}
Will output:
Value = 6.8872pt
Note:
Values of lengths are stored as points, and 1 inch ≈ 72.27 pt
This does not require any additional packages.
Update:
Use \hbox to correctly calculate the height of a different sized environment, but it won't work with newlines :-(

LaTeX table positioning

I have a LaTeX document that contains a paragraph followed by 4 tables followed by a second paragraph. I want the 4 tables to appear between the two paragraphs which from what I've read means I should use the [h] option after beginning the table environment (e.g. \begin{table}[h]).
Using this the first two tables appear after paragraph 1 as expected, however paragraph 2 is then displayed with the last two tables appearing on the following page. How can I get the tables to appear in the correct location?
I've tried various things to correct the positioning such as using [h!] however this doesn't seem to have any effect. Using \clearpage after the tables does have the desired effect of making the tables appear before the second paragraph but it then leaves the last two tables on their own page with loads of white-space, when I would prefer to have the second paragraph begin immediately after the tables.
Paragraph 1...
\begin{table}[h]
table1...
\end{table}
\begin{table}[h]
table2...
\end{table}[h]
...
Paragraph 2...
After doing some more googling I came across the float package which lets you prevent LaTeX from repositioning the tables.
In the preamble:
\usepackage{float}
Then for each table you can use the H placement option (e.g. \begin{table}[H]) to make sure it doesn't get repositioned.
Table Positioning
Available Parameters
A table can easily be placed with the following parameters:
h Place the float here, i.e., approximately at the same point it occurs in the source text (however, not exactly at the spot)
t Position at the top of the page.
b Position at the bottom of the page.
p Put on a special page for floats only.
! Override internal parameters LaTeX uses for determining "good" float positions.
H Places the float at precisely the location in the LATEX code. Requires the float package. This is somewhat equivalent to h!.
If you want to make use of H (or h!) for an exact positioning, make sure you got the float package correctly set up in the preamble:
\usepackage{float}
\restylefloat{table}
Example
If you want to place the table at the same page, either at the exact place or at least at the top of the page (what fits best for the latex engine), use the parameters h and t like this:
\begin{table}[ht]
table content ...
\end{table}
Sources: Overleaf.com
At the beginning with the usepackage definitions include:
\usepackage{placeins}
And before and after add:
\FloatBarrier
\begin{table}[h]
\begin{tabular}{llll}
....
\end{tabular}
\end{table}
\FloatBarrier
This places the table exactly where you want in the text.
Here's an easy solution, from Wikibooks:
The placeins package provides the command \FloatBarrier, which can be used to prevent floats from being moved over it.
I just put \FloatBarrier before and after every table.
What happens if the text plus tables plus text doesn't fit onto a single page? By trying to force the typesetting in this way, you are very likely to end up with pages that run too short; i.e., because a table cannot by default break over a page it will be pushed to the next, and leave a gap on the page before. You'll notice that you never see this in a published book.
The floating behaviour is a Good Thing! I recommend using [htbp] as the default setting for all tables and figures until your document is complete; only then should think about fine-tuning their precise placement.
P.S. Read the FAQ; most other answers here are partial combinations of advice given there.
If you want to have two tables next to each other you can use: (with float package loaded)
\begin{table}[H]
\begin{minipage}{.5\textwidth}
%first table
\end{minipage}
\begin{minipage}{.5\textwidth}
%second table
\end{minipage}
\end{table}
Each one will have own caption and number.
Another option is subfigure package.
In my case I was having an issue where the table was not being displayed right after the paragraph I inserted it, so I simply changed
\begin{table}[]
to
\begin{table}[ht]
You may want to add this to your preamble, and adjust the values as necessary:
%------------begin Float Adjustment
%two column float page must be 90% full
\renewcommand\dblfloatpagefraction{.90}
%two column top float can cover up to 80% of page
\renewcommand\dbltopfraction{.80}
%float page must be 90% full
\renewcommand\floatpagefraction{.90}
%top float can cover up to 80% of page
\renewcommand\topfraction{.80}
%bottom float can cover up to 80% of page
\renewcommand\bottomfraction{.80}
%at least 10% of a normal page must contain text
\renewcommand\textfraction{.1}
%separation between floats and text
\setlength\dbltextfloatsep{9pt plus 5pt minus 3pt }
%separation between two column floats and text
\setlength\textfloatsep{4pt plus 2pt minus 1.5pt}
Particularly, the \floatpagefraction may be of interest.
Not necessary to use \restylefloat and destroys other options, like caption placement. just use [H] or [!h] after \begin{table}.

How do I indicate an en-dash while in math mode using LaTeX?

There are four kinds of 'dashes' in LaTeX: hyphen, en-dash(--), em-dash(---) and minus $-$. They are used for, respectively, hyphenation and joining words, indicating a range, punctuation, and a mathematical symbol. My question is: how do I indicate an en-dash (range) in math mode? (as in $S=1 to 2$)? Do I have to drop out of math mode in the middle of the 'equation' ($S=1$--$2$)? Or is there a symbol I can use and stay in math mode? I tried $S=1\--2$ but this gives me a minus, not a en-dash, and $S=1--2$ gives two minuses. My guess is I am going to have to drop out of math mode but maybe there is a way to do it without that.
The simplest way is to use $S=1\mbox{--}2$. If you already have \usepackage{amsmath} in your document's preamble, however, you're better off using \text: $S=1\text{--}2$ because \text will adjust the size of the font when used in super- and subscripts: $S_{1\text{--}2}=0$.
An en dash used in math may easily be confused with a minus sign. You may want to look at other techniques for indicating a range such as ellipses (\ldots for dots on the baseline [used between commas], or \cdots for centered dots [used between centered operators such as plus signs]) or using the bracket notation. Some examples:
$S = \{1, 2, \ldots, n\}$ indicates an element in the set containing integers between 1 and $n$.
$S = [0, 1]$ indicates a real number between 0 and 1 (inclusive).
You can use \textrm which does not depend on \usepackage{amsmath}.
Some examples:
$S=1\textrm{--}2$
$S_{1\textrm{--}2}=0$ % correctly changes the font size for subscript
$S_{1\textrm{2}2}=0$ % does not change the font size :(
Or maybe use \textnormal instead of \textrm.
See also LaTeX: use \textnormal instead of \textrm (or \textsf) in math.
Well, you could use $1\mbox{--}2$, but I'd look at the list of math symbols in the symbol list. Hmm. I don't find one.
Use the command \leftrightline in the package MnSymbol. I don't know if it's the length needed for your purposes, but hopefully it will do the trick.

How do I prevent LaTeX from padding spaces between paragraphs so that next section begins at top of next page?

I have a two-column paper where space restrictions are very tight.
I just looked at my last version of the manuscript and saw that the upper half contains a figure (as expected), but in the lower half there is a lot of vertical space between paragraphs (enough to squeeze 10 more lines), and that LaTeX probably added it so that in the beginning of the next page a new numbered section will begin at the top of the page.
I know there's a way to adjust this so LaTeX doesn't try so hard, but I'm not sure how. any help? Thanks!
The parameter that controls inter-paragraph spacing is called \parskip(See Paragraph Spacing ). You set it (with "rubber" values) using something like:
\setlength{\parskip}{1cm plus4mm minus3mm}
The defualt value of \parskip is class dependent. The "plus" and "minus" parts tell TeX how much it can adjust the value to improve the layout (that is they make the spacing elastic, thus the "rubber" designation). Reducing (or eliminating) the "plus" part of the rubber might help.
Watch out though, you can cause other layout artifacts if you constrain TeX too much.
Other things to think about:
The widow and club penalties probably apply section headings, and may be affecting TeX's layout choices (see https://stackoverflow.com/questions/512967/how-can-one-keep-a-section-from-being-at-the-end-of-a-page-in-latex for a discussion).
You may also want to consider messing with \baselineskip which controls the allowed spacing between lines of text and can also have rubber values.
This is a common problem, and there are probably some fairly sophisticated treatments already prepared on CTAN.
\vfill before the new section worked perfectly for me.

Resources