what does this kinds of if mean? - latex

I saw a config file on:
https://github.com/tzengyuxio/pages/blob/gh-pages/pandoc/pm-template.latex
In this file, there is some kinds of "if" that make me confused.
$if(fontsize)$$fontsize$,$endif$ % In my mind $xxx$ means a mathematical formula, but why does it appear in if-else statement?
\ifnum 0\ifxetex 1\fi\ifluatex 1\fi=0 % What does this means? Does the 0, 1, 1 mean argument for commands ifnum, ifxetex and ifluatex? Shouldn't the arguments be in braces "{}" ? And what does it mean together?
And there is another kind of if: \IfFileExists{upquote.sty}{\usepackage{upquote}}{} % Why does this command have so many braces?
Why is there so many kinds of if? What is the differences of them?

Related

wxMaxima: "*" wrong number of arguments

I used texput to redefine the tex1 output for *:
texput("*", "", infix);
but this causes an error when there are more than two terms:
tex1(a*b);
> ab
tex1(a*b*c);
> "*": wrong number of arguments.
Any idea why this is happening or how I might work around it?

Problem with do if command in spss: "expression is incomplete"

I would like to compute in spss a multiple if condition. My data looks like this:
DO IF (A<75 & DN<75).
COMPUTE AD=0.
else if (A=75 & DN<75).
COMPUTE AD=(A-75).
else if (A<75 & DN=75).
COMPUTE AD=(DN-75).
else if (A=75 & DN=75).
COMPUTE AD=[(A-75)+(DN-75)].
END IF.
EXECUTE.
It gives me an error at the last compute command, saying:
The expression is incomplete. Check for missing operands, invalid operators,
unmatched parentheses or excessive string length.
Execution of this command stops.
Can anyone tell me please how should I formulate the equation in order to be acceptable to spss?
Right, square and curly brackets are not allowed in COMPUTE expressions, only parentheses. Spaces in between nested parentheses are allowed, but not required.

Building Latex/Tex arguments in lua

I use lua to make some complex job to prepare arguments for macros in Tex/LaTex.
Part I
Here is a stupid minimal example :
\newcommand{\test}{\luaexec{tex.print("11,12")}}% aim to create 11,12
\def\compare#1,#2.{\ifthenelse{#1<#2}{less}{more}}
\string\compare11,12. : \compare11,12.\\ %answer is less
\string\test : \test\\ % answer is 11,12
\string\compare : \compare\test. % generate an error
The last line creates an error. Obviously, Tex did not detect the "," included in \test.
How can I do so that \test is understood as 11 followed by , followed by 12 and not the string 11,12 and finally used as a correctly formed argument for \compare ?
There are several misunderstandings of how TeX works.
Your \compare macro wants to find something followed by a comma, then something followed by a period. However when you call
\compare\test
no comma is found, so TeX keeps looking for it until finding either the end of file or a \par (or a blank line as well). Note that TeX never expands macros when looking for the arguments to a macro.
You might do
\expandafter\compare\test.
provided that \test immediately expands to tokens in the required format, which however don't, because the expansion of \test is
\luaexec{tex.print("11,12")}
and the comma is hidden by the braces, so it doesn't count. But it wouldn't help nonetheless.
The problem is the same: when you do
\newcommand{\test}{\luaexec{tex.print("11,12")}}
the argument is not expanded. You might use “expanded definition” with \edef, but the problem is that \luaexec is not fully expandable.
If you do
\edef\test{\directlua{tex.sprint("11,12")}}
then
\expandafter\compare\test.
would work.

\newcommand / \newenvironment - optional parameters

I'm experimenting with my own commands and environments and now I'm facing those problems:
How to create command \foo{parameter}[optional] or environment called \begin{bar}{parameter}[optional]?
How to create command \foo[optional_1]...[optional_n]{parameter}
I've tried
\newcommand{\foo}[3][][]{#1#2#3} - failed
\newcommand{\foo}[3][2][][]{#1#2#3} - failed
Does anyone know some hint? Thanks a lot.
You can't create a \foo{parameter}[optional] command simply; you can, however, create a \foo[optional]{parameter} command with
\newcommand{\foo}[2][default]{Mandatory: #2; optional: #1}
If you call it as \foo{given}, it will produce Mandatory: given, optional: default; if you call it as \foo[bonus]{given}, it will produce Mandatory: given, optional: bonus. This is probably how you should do it—that will look better with the rest of your LaTeX code. Creating a new environment with optional parameters is done similarly with
\newenvironment{env}[2][def]{(#1,#2)\begingroup}{\endgroup}
where #1 is again the optional argument; this is again written as \begin{env}[opt]{req}...\end{env}. If you really want a command in the other form, see the end of my answer.
The TeX FAQ has an answer about writing commands with more than one optional argument. There are two options to how to do it. The underlying idea is to define a command which takes an optional argument, and then runs another command which itself takes an optional argument, etc.; the twoopt package encapsulates this.
If you really want a command like \reversed{mandatory}[optional], you can do it like so. First, you define a command which takes a required argument, stores it in a macro, and then forward it onto another command. This second command takes an optional argument, and uses the defined command and the optional argument. Putting this all together, we get
\makeatletter
\newcommand{\reversed}[1]{\def\reversed#required{#1}\reversed#opt}
\newcommand{\reversed#opt}[1][def]{Required: \reversed#required; optional: #1}
\makeatother
You can then use \reversed{mandatory}[optional] or just \reversed{mandatory}, and everything should work.
Using the xparse package (part of the LaTeX3 development efforts):
\usepackage{xparse}
\NewDocumentCommand\foo{O{}O{}m}{%
% Code with optional #1 and #2 with empty defaults
}
\NewDocumentCommand\foo{mO{}}{%
% Code with optional #2 with empty default
}
\NewDocumentEnvironment{foo}{O{}}{%
% Start code with optional #1
}{%
% End code with optional #1
}
Optional arguments are a bit different in xparse to with \newcommand. You can detect whether one is given or not:
\NewDocumentCommand\foo{mo}{%
\IfNoValueTF{#2}
{Code without #2}
{Code with #2}%
}
You'll see that this works by using a lower case 'o', whereas the upper case 'O' then requires a default value (which I've made empty by including an empty group).
Consider also the xargs package. The following is an example from its documentation.
Set it up in the usual way,
\usepackage{xargs}
and then if you define
\newcommandx*\coord[3][1=1, 3=n]{(#2_{#1},\ldots,#2_{#3})}
(which means to use "1" for the first argument, if it is not specified, and to use "n" for the third). Then
$\coord{x}$
yields (sans subscripts)
(x1, . . . , xn)
and
$\coord[0]{y}$
yields (again, sans subscripts, and y replaces the mandatory parameter)
(y0, ..., yn)
I know there are already comprehensive answers, but is some cases, I want to give different definitions for different situations. There is a still very basic yet simple solution for this. I write it down in case any other need it.
% ----------------------------------
%! TEX program = XeLaTeX
% !TeX encoding = UTF-8
% Author: Troy_Daniel
% Email: Troy_Daniel#163.com
% ----------------------------------
\documentclass{article}
\usepackage{xcolor}
\newcommand{\Caption}[3]{%
\textcolor[rgb]{0.36, 0.72, 0.80}{\Large #1}
{\def\tmp{#3}
\ifx\tmp\empty % the third parameter is not provieded
\textcolor[rgb]{0.96, 0.66, 0.35}{\small#2}
\else % non-empty third parameter
\textcolor[rgb]{0.96, 0.66, 0.35}{\small[#3]#2}
\fi}}
\begin{document}
\Caption{First}{Second}{}
\Caption{First}{Second}{Third}
\end{document}
And the result is shown below, different definitions for optional parameter(s):

LaTeX \newcommand default argument: is empty?

I'm trying to write a simple example command that prints nothing without an argument, but with an argument it surrounds it with something.
I've read that the default value should be \#empty and the simple \ifx\#empty#1 condition should do the job:
\newcommand{\optarg}[1][\#empty]{%
\ifx\#empty#1 {} \else {(((#1)))} \fi
}
\optarg % (((empty)))
\optarg{} % (((empty)))
\optarg{test} % (((empty))) test
The latter three commands all print the empty word for some reason, and I want the first two to print nothing and the last to print (((test))).
I'm using TeXLive/Ubuntu. An ideas?
Try the following test:
\documentclass{article}
\usepackage{xifthen}% provides \isempty test
\newcommand{\optarg}[1][]{%
\ifthenelse{\isempty{#1}}%
{}% if #1 is empty
{(((#1)))}% if #1 is not empty
}
\begin{document}
Testing \verb|\optarg|: \optarg% prints nothing
Testing \verb|\optarg[]|: \optarg[]% prints nothing
Testing \verb|\optarg[test]|: \optarg[test]% prints (((test)))
\end{document}
The xifthen package provides the \ifthenelse construct and the \isempty test.
Another option is to use the ifmtarg package (see the ifmtarg.sty file for the documentation).
Using the LaTeX3 xparse package:
\usepackage{xparse}
\NewDocumentCommand\optarg{g}{%
\IfNoValueF{#1}{(((#1)))}%
}
In the underlying TeX engine with which LaTeX is written, the number of arguments a command can take is fixed. What you've done with the default [\#empty] is ask LaTeX to examine the next token to see if it is an open square bracket [. If so, LaTeX takes the contents of square brackets as the argument, if not, the next token is put back into the input stream and the default \#empty argument is used instead. So to get your idea to work, you have to use square brackets to delimit the optional argument when present:
\optarg
\optarg[]
\optarg[test]
You should have better luck with this notation.
It's annoying that you can't use the same brackets for an optional argument as you use for a required argument, but that's the way it is.
\documentclass{article}
\usepackage{ifthen} % provides \ifthenelse test
\usepackage{xifthen} % provides \isempty test
\newcommand{\inlinenote}[2][]{%
{\bfseries{Note:}}%
\ifthenelse{\isempty{#1}}
{#2} % if no title option given
{~\emph{#1} #2} % if title given
}
\begin{document}
\inlinenote{
simple note
}
\inlinenote[the title]{
simple note with title
}
\end{document}

Resources