\newenvironment{nameOfEnvironment}[1][]%
Can someone explain the empty bracket?
You should consider reading Is there a comprehensive and complete LaTeX reference? where you'll find information on all sorts of LaTeX2e sources.
Technically, \newenvironment{<cmd>}[<num>][<default>]{<beg-def>}{<end-def>} uses \newcommand as base, so understanding the latter will help you understand the former.
Specific to your case, LaTeX2e for authors user guide mentions the following about \newcommand:
...the command:
\newcommand{<cmd>}[<num>][<default>]{<definition>}
defines <cmd> to be a command with <num> arguments, the first of which is
optional and has default value <default>.
Note that there can only be one optional argument but, as before, there can be
up to nine arguments in total.
So,
\newenvironment{nameOfEnvironment}[1][]%
{<beg-def>}
{<end-def>}
defines an environment nameOfEnvironment that takes a single argument (as a result of [1]). This single argument is an optional argument (as a result of the second []) that, if not specified, has an empty default value.
You would be able to use it as
\begin{nameOfEnvironment}
<stuff>
\end{nameOfEnvironment}
or
\begin{nameOfEnvironment}[something]
<stuff>
\end{nameOfEnvironment}
In the former case, the optional argument #1 is empty, while the second has an optional argument value of something.
The following explanation is taken from LaTeX: Structured documents for TeX (unofficial LaTeX reference manual):
13.5 \newenvironment & \renewenvironment
Synopses:
\newenvironment[*]{env}[nargs][default]{begdef}{enddef}
\renewenvironment[*]{env}[nargs]{begdef}{enddef}
These commands define or redefine an environment env, that is, \begin{env} ... \end{env}.
*
The *-form of these commands requires that the arguments (not the contents
of the environment) not contain multiple paragraphs of text.
env
The name of the environment. For \newenvironment, env must not be
an existing environment, and the command \env must be undefined. For
\renewenvironment, env must be the name of an existing environment.
nargs
An integer from 1 to 9 denoting the number of arguments of the newly-defined
environment. The default is no arguments.
default
If this is specified, the first argument is optional, and default gives the default value for that argument.
begdef
The text expanded at every occurrence of \begin{env}; a construct of the form #n in begdef is replaced by the text of the nth argument.
enddef
The text expanded at every occurrence of \end{env}. It may not contain any
argument parameters.
Related
The following is my sample code: https://play.openpolicyagent.org/p/oyY1GOsYaf
Here when I try to evaluate names array, it is showing:
error occurred: 1:1: rego_unsafe_var_error: var names is unsafe
But when I define the same comprehension outside the allow rule definition : https://play.openpolicyagent.org/p/Xv0cF7FM8b, I am able to evaluate the selection
[
"smoke",
"dev"]
could someone help me to point out the difference and if I want to define the comprehention inside the rule is there any syntax I need to follow? Thanks in advance
Note: I am getting the final output as expected in both cases, only issue is with the names array evaluation.
The way the Rego Playground generates a query when evaluating a selection is much more simplistic than one might assume. A query will be generated from your selected text, without taking into account where in the document that text was selected. This means that even if you select a local variable inside a rule body, the query will simply contain that variable name (names, in your case); which will be perceived as a reference to a top-level variable in the document's body, even though a rule-local variable was selected. This is why your first sample returns an error, as there is no top-level variable names in the document; whereas the second sample does, and therefore succeeds.
You can test this quirk by selecting and evaluating the word hello on line 3 here: https://play.openpolicyagent.org/p/n5OPoFnlhx.
package play
# hello
hello {
m := input.message
m == "world"
}
Even though it's just part of a comment, it'll evaluate just as if you had selected the rule name on line 5.
According to the user guide it is possible to assign values to variables and then perform simple arithmetic.
Imagine I have fixture designed to take an element on the page and extracting the numerical value as a Double (i do this now using the HSAC Slim BrowserTest fixture and my own code)
|script |numbers extraction |
|$testval1=|numeric value of |element1 | |
|$testval2=|numeric value of |element2 | |
Running this gives me something like:
|script |numbers extraction |
|$testval1<-[20.04]|numeric value of |element1 ->[€ 20,04] | |
|$testval2<-[5.1] |numeric value of |element2 ->[€ 5,1] | |
Now say I want to compare the sum of the two doubles with the numeric value of a third element:
|script|numbers extraction |
|check |numeric value of |element3|{=${ ${testval1} + ${testval2} =}|
No matter what combination of parentheses and dollar-signs I use in the last cell, I always get 'invalid expression'.
${= $testval1 + $testval2 =} invalid expression: $testval1 + $testval2
{${=$testval1 + $testval2 =}} {invalid expression: $testval1 + $testval2}
${=${testval1} + ${testval2} =} [invalid expression: undefined variable: testval1 + undefined variable: testval2]
${= !-$testval1-! + !-$testval2-! =} invalid expression: $testval1 + $testval2
${= !-${testval1}-! + !-${testval2}-! =} invalid expression: ${testval1} + ${testval2}
Running the last line (without parenthesis around testval1 and testval2) returns:
|check|numeric value of|element3 ->[€ 25.14]| [25.14] expected [invalid expression: $testval1->[20.04] + $testval2->[5.1]] |
Unfortunately you can can't do what you are looking for. The variables you assign the value of the elements to are actually SLIM symbols, and not variables at the wiki level. If you scroll a bit down on the user guide page you linked to in the question you will find a section called "Difference between variables and SLIM symbols":
Variables are evaluated at render time, before the test executes. This allows for values to be set based on page hierarchy and other things that are purely inputs to the tests.
Symbols only exist at execution time. They can be changed at runtime, so are distinct from variables, which cannot.
I find the three types of variables in FitNesse/SLIM are confusing to people and their different usage, syntax and possibilities cause many issues. My understanding is:
Markup variables (aka wiki variables). For instance ${myVar}, defined using !define. They get their value at page render time, so even before a test is started, so you see their value when you browse to a wiki page, and only in the page's source do you see it is a variable. These can be used in markup expressions, which is what you are trying to do in the question.
Scenario parameters. For instance #{myVar} (or #myVar), defined in the first row of a SLIM scenario table. These are the 'formal parameters' to the scenario, which get their actual value based on the invocation of the scenario (i.e. each usage of the scenario, either from a script table, other scenario or row in a decision table defines their value). They get their value at the start of a test, before its first action is performed. You see the variable when you look at the scenario table that defines it. (When you use the 'table template' table type defined by hsac-fitnesse-plugin (which is included in hsac-fitnesse-fixtures project baseline) you don't need to define the variable names in the first row of the table, they are automatically found based on their occurrence (e.g. #{myVar}) inside the table.)
SLIM symbols. For instance $myVar, they are assigned their value using $myVar=. These are 'runtime variables' that get their value during test execution, they are global to a test suite and their value might be changed during test execution. These are the only kind of variables that can get their value from a property obtained from the 'system under test', and they are the variables you are using in your question's tables. They are actually references to objects inside the SLIM process so fixtures might change the internal state of the object the variable refers to, without this change showing in the wiki representation of the variable (which is just the object's toString() result at time of last assignment).
P.S. The conversion of a string to a double does not require a custom fixture (like your numbers extraction) when you using the hsac-fitnesse-fixtures. You could just use the convert to double method of the library's string fixture.
You seem to be using browser test, this is an HSAC installation I take it? Please mention this in the question as HSAC is a FitNesse fixture.
Anyway, removing the curly brackets should do the trick. With curly brackets it is expecting a global variable, those that are implemented using !define var {foo}
When using variables that are locally defined such as with |$bar=|value of|foo| have to be called in the test using the variable without curly brackets.
|$bar=|value of|foo|
|enter|$bar|as|inputField|
Find more stuff on HSAC usage here: https://github.com/fhoeben/hsac-fitnesse-fixtures/wiki/2.-Slim-Fixtures
Sidenote:
Then there are also table templates that use #var or #{var}, where the use of #{var} is preferred because #{var} will look for the column var and #var will accept a column v or va, if you happen to implement that. Using curly brackets here ensures the full variable name is used.
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.
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):
I spent some time trying to write a 'helper' macro to test a parameter for a new value, else use the existing value -- default values exist for all parameter positions.
I wanted to be able to write:
\foo{left}{nil}{}{20pt}
so that the second parameter would used its current value but the third value would be the value empty string. I wanted to use the notation:
\edef\pA{\isnil{#1}{\pA){#1}} % one for each parameter
I defined \isnil like so:
\def\nil{nil}
\def\isnil#1#2#3{%
\edef\nilTest{#1}%
\ifx\nilTest\nil#2\else#3\fi
}
but when I tried to run it, TeX complained that \nilTest is an undefined control sequence.
That is true of course, but I want \pA to hold a value, not a recipe for a value, so it must be an \edef which means that all the macro test will be expanded but while will the \edef not protect the \nilTest -- is this a place to use \noexpand -- that did not seem to work for me.
EDIT: no digits in \cs names (yeah, I knew that.)
Why doesn't your solution work? \edef\pA{\isnil{#1}{\pA){#1}} expands \isnil and gets \edef\nilTest{.... Now \edef is not expandable and falls into a sequence of \pA as the first element. An attempt to expand the next macro \nilTest fails.
Use \setpar from the following code to change your parameter.
\def\nil{nil}
\def\setpar#1#2{%
\edef\nilTest{#2}%
\ifx\nilTest\nil\else\let#1\nilTest\fi}
\def\first{old first}
\def\second{old second}
\setpar \first{nil}
\setpar \second{new}
first = ``\first'', second = ``\second''
P.S. Do not use digits in your macro.