How can I calculate with optional parameter in \newcommand block? - latex

I want to have a grid-command which is adjustable to the wanted height. Here is what I did:
\newcommand{\vhlines}[1]{
\hspace{1em}
\setlength{\unitlength}{0.75cm}
\begin{picture}(22,#1)
\color{lightgray}
\linethickness{0.075mm}
\multiput(0,0)(1,0){21}
{\line(0,1){#1}} % need to subtract 1 from #1
\multiput(0,0)(0,1){#1}
{\line(1,0){20}}
\end{picture}
}
If I call, e.g., \vhlines{16} I find the vertical lines to be too long on the upper end. They are correct if I write a 15 instead of the #1 in the line with the comment.
Is there an elegant way to do this?

You can perform elementary numeric (integer) expressions using \numexpr:
\documentclass{article}
\usepackage{xcolor}
\newcommand{\vhlines}[1]{%
\hspace{1em}%
\setlength{\unitlength}{0.75cm}%
\begin{picture}(22,#1)
\color{lightgray}
\linethickness{0.075mm}
\multiput(0,0)(1,0){21}
{\line(0,1){\numexpr#1-1}}
\multiput(0,0)(0,1){#1}
{\line(1,0){20}}
\end{picture}
}
\begin{document}
\noindent
\vhlines{4}
\end{document}
Alternatively, for more complex expressions, add to your preamble
\usepackage{xparse}
\ExplSyntaxOn
\cs_new_eq:NN \calc \fp_eval:n
\ExplSyntaxOff
which would allow you to use \calc{<your numerical expression>}.

Related

Using numbers as input in \newenvironment LaTeX

I'm trying to make an environment that builds a column vector, and that takes as input the scale factor of the distancing between the rows:
\newenvironment{VEC}[1]{\begin{Bmatrix}\renewcommand{\arraystretch}{#1}}
{\end{Bmatrix}}
I know that the \newenvironment command accepts as input only strings, so I tried with \value{#1} having no successfull result. Any help would be appreciated.
Nothing to do with newenvironment or arguments, you just need to switch the order:
\documentclass{article}
\usepackage{mathtools}
\newenvironment{VEC}[1]{\renewcommand{\arraystretch}{#1}\begin{Bmatrix}}
{\end{Bmatrix}}
\begin{document}
\[
\begin{VEC}{3}
4\\
6\\
\end{VEC}
\]
\end{document}

Latex pick specific element of an array of strings based on variable

Here's my setup. I want to set a integer variable called intoption at the top of my latex document.
\newcommand{\intoption}{3} % This variable can be 1,2,3 or 4
I also want to define a command called pickoption that will use the intoption value, say value x, to pick the x-th element of the array. So
\pickoption{First}{Second}{3rd}{Last}
will return the text "3rd". Is this behavior possible? How would I define pickoption?
One of many possibilities:
\documentclass{article}
\newcommand{\intoption}{3}
\newcommand{\pickoption}[4]{
\ifnum\intoption=1
#1
\else
\ifnum\intoption=2
#2
\else
\ifnum\intoption=3
#3
\else
\ifnum\intoption=4
#4
\fi
\fi
\fi
\fi
}
\begin{document}
\pickoption{First}{Second}{3rd}{Last}
\end{document}

\mathnormal bold in LaTeX

How do I write this type of mathematical letter in LaTeX? I have tried \mathnormal but I can not make it bold.
To have the exact same type of bold and cursive x, then you can use $\boldsymbol{x}$. However, if all you want is a bold (but upright x), you can use \mathbf{ }.
By the way, if you are having to write a bunch of these, then I usually define a shorthand command:
\newcommand{\+}[1]{\ensuremath{\boldsymbol{#1}}}
which you can then write your example as:
$\+x_1, \+x_2, \ldots, \+x_n$
\documentclass{article}
\usepackage{amsmath}
\begin{document}
\[
\boldsymbol{x}_{1}, \boldsymbol{x}_{2}, \dots ,\boldsymbol{x}_{3}
\]
\end{document}

\afterpage and \endfloat

How do I combine \afterpage and \endfloat to easily switch between having figures and tables at the end of the document or having them in the text?
I want to easily choose between my figures at the end of the document and my figures in the text. Because of that, sometimes I will use \afterpage package and other times I will use \endfloat would be nice to combine both.
Right now, all the times I try to run \endfloat when I have a clear page, I get the following message:
Argument of \efloat#xfloat has an extra }.
I already tried to include after page in the DeclareDelayedFloatFlavor, something like:
\DeclareDelayedFloatFlavor{afterpage}{figure}
It did not work.
\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{float}
\usepackage{afterpage}
% ---------------------
%figures at the end
% ---------------------
\usepackage[nolists]{endfloat}
% force landscape at the end
\begin{document}
{\afterpage{
\begin{figure}
\end{figure}
}
\end{document}
If you want to switch between having figures within the text and at the end, I suggest to only use the endfloat package. Commenting or commenting it's optional argument disable will allow you to quickly alternate between figures at the end or in the text.
I'm not entirely certain what the purpose of afterpage was in your example, but if you used it to move the figure to a separate page, this can conveniently be done with the p floating specifier.
\documentclass{article}
\usepackage[
disable
]{endfloat}
\begin{document}
test
\begin{figure}[p]
xxx
\caption{caption}
\end{figure}
test
\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