How to perform "complex" calculations in LaTeX/TikZ - latex

I expect this question has already been answered a thousand times, but I just cannot find a solution to my issue =(
I want to calculate some values in LaTeX (TikZ), some of which are lengths (e.g. 10 pt), some are not. As long as calculations are of very simple forms like a*b+c everything is fine, but if I need brackets like (a+b)*(c+d) LaTeX complains. Furthermore, if I have nested definitions, these are not solved as expected. Example:
\def\varA{1+2}
\def\varB{10}
% I want to calculate (1+2)*10 or 10*(1+2), respectively.
\def\varC{\varA*\varB} % evaluates to 21
\def\varD{\varB*\varA} % evaluates to 12
So basically my question is: What is the correct (or recommended) way to do calculations in LaTeX?
As a more realistic example, here is something I actually want to do and just can't:
% total height of my TikZ image
\def\myheight{20ex}
% five nodes shall be drawn as a stack
\def\numnodes{5}
% but there shall be some space at the top (like a 6th node)
\def\heightpernodeA{\myheight / (\numnodes + 1)} % fails whenever I want to use it
% workaround?
\def\numnodesplusone{\numnodes + 1}
\def\heightpernodeB{\myheight / \numnodesplusone} % fails for the reason explained above
Unfortunately I can not redefine \numnodes to be 6 as I use the variable for various calculations.
best regards

You can use \pgfmathsetmacro for more complex calculation. You'll need to remove the unit in myheight though:
% total height of my TikZ image
\def\myheight{20}
% five nodes shall be drawn as a stack
\def\numnodes{5}
% but there shall be some space at the top (like a 6th node)
\pgfmathsetmacro\heightpernodeA{\myheight / (\numnodes + 1)}
You can add the unit back when you use it: \draw (0,0) -- ({\heightpernodeA ex},{1});

Related

Uneven text in Latex, although I used the package ragged2e

Here is the latex code:
\documentclass[12pt]{article}
\date{}
\usepackage{graphicx}
\usepackage{setspace}
\usepackage{blindtext}
\usepackage{indentfirst}
\usepackage{babel,etoolbox, lineno}
\usepackage[natbib=true,style=ext-numeric,sorting=none]{biblatex}
\addbibresource{name.bib}
\usepackage{color}
\usepackage[utf8]{inputenc}
\usepackage[document]{ragged2e}
\setstretch{1.5}
\usepackage{geometry}
\setlength{\parindent}{4em}
\usepackage{etoolbox}
\begin{document}
\par The value of "a" is the number of times the word occurs in the analysis text. The value of "b" is the number of times the word occurs in the reference text. The value of "c" is the total number of words in the analysis text. The value of "d" is the total number of words in the reference text.\par
Given this contingency table, WordHoard calculates the log-likelihood ratio statistic G2 to assess the size and significance of the difference of a word's frequency of use in the two texts. The log-likelihood ratio measures the discrepancy of the the observed word frequencies from the values which we would expect to see if the word frequencies (by percentage) were the same in the two texts. The larger the discrepancy, the larger the value of G2, and the more statistically significant the difference between the word frequencies in the texts. Simply put, the log-likelihood value tells us how much more likely it is that the frequencies are different than that they are the same.\par
When you compute many log-likelihood ratio values some will appear significant by chance alone. For example, if we compute one thousand log-likelihood values, we can expect about ten of them to exceed the breakpoint value of 6.63 by chance. This may lead us to accept a frequency difference as significant when it is really just a chance fluctuation.\par
One way to deal with this problem is to "up the ante" by adjusting the breakpoint values for the number of comparisons. WordHoard allows this as an option. The method used by WordHoard is called the Sidak correction. It is very conservative, particularly as the total number of comparisons increases.\par
As an example, let's see how comparing word form frequencies reveals some properties of Shakespeare's play "Othello, the Moor of Venice." The comparison we select will highlight lemmata that are disproportionately common or rare in Othello with respect to Shakespeare's tragedies as a whole. To compare the lemmata counts, select "Compare Many Word Forms" from the Analysis menu. WordHoard displays the following dialog.
\end{document}
The outputted text is completely uneven, every line ends somewhere else, looks completely unformatted. I put \par before each paragraph. What could be the issue?
The ragged2e package's purpose is to have a ragged edge (vs block). If you don't want uneven line endings, don't use that package.
When I remove \usepackage{ragged2e} from your code, I get one line that's too long. It doesn't get hyphenated cause LaTeX doesn't know which language your text is in. Use the babel package for hyphenation, e.g., add \usepackage[american]{babel} in the packages list. With babel and without ragged2e I get block mode with all lines equally long.

Font scaling, variables, calculations in LaTeX

I'm currently writing my CV and I'm running out of space. I wish to change the font in the whole CV template, but I want to do it proportionally. For example, some headings are 12pt, subheadings are 11pt. I want to be able to change them to 0.9 * 12 pt, 0.9 * 11 pt, aka to scale them accordingly. I want to change
\fontsize{12pt}{1em}
to
\fontsize{(0.9 * 12)pt}{1em}
, where \fontsize is a command from anyfontsize package.
But I end up with weird results and lots of logs/mistakes. Is there a way to ensure that '*' will produce a number, like in "regular" programming languages?
Other things I've tried include:
\newcommand{\myfont}[2]{\fontsize{0.9*#1pt}{0.9*#2}em}
\myfont{12}{1}
But it doesn't work the way I expect it to. I also tried adding brackets () and making a new command \newcommand{\mymultiply}[2]{#1*#2}
Is there an elegant and neat way to do this? Is there a "regular" way in LaTeX of doing actual calculations, or should I avoid this approach? Thanks in advance!
3 possible approaches:
I would avoid using hard coded font sizes altogether. If you instead us font commands like \smaller or \larger you can simply change the overall size with documentclass options like 10pt or 12pt and all the fonts in the document will scale, including sections etc.
if you store your base fontsize in a length, you could use \dimexpr to make the calculations
if for whatever reason you don't want to create a new length, you use an expl3 command to do the calculations
\documentclass[
%10pt
12pt
]{article}
\usepackage{anyfontsize}
\ExplSyntaxOn
\cs_new_eq:NN \fpeval \fp_eval:n
\ExplSyntaxOff
\begin{document}
\section{section}
section
\newlength{\foo}
\setlength{\foo}{12pt}
\fontsize{\dimexpr.9\foo\relax}{\dimexpr1.08\foo \relax}\selectfont
test
\fontsize{\dimexpr\fpeval{.9*12}pt\relax}{\dimexpr\fpeval{.9*1.2*12}pt\relax}\selectfont
test
\end{document}

How to make all numbers and letter take up the same amount of space in 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>.

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 :-(

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.

Resources