Tag Info

Hot answers tagged

6

As in almost any programming language, \if... statements can be nested, however, there's no \elseif, so \else \if.... has to be used and concluded with \fi. A new \if... variable (well, macro actually) can be defined with \newif\ifsomename which is initially set to the false state. \somenametrue will set to true, \somenamefalse will set it false. Here's ...


5

Multiple conditions can be handled with plain TeX conditionals using \ifnum, for example \ifnum0% \iffirstsomething 1\fi \ifsecondsomething 1\fi >0 % % Do stuff \fi which will only do the stuff if at least one condition is met. Similarly \ifnum0% \iffirstsomething\else1\fi \ifsecondsomething\else1\fi =0 % % Do stuff \fi will be true ...


5

In the example below, you do have to generate \ifvone, etcetera, but only have to turn on the one(s) that you want. \documentclass{book} \newif\ifvone \newif\ifvtwo \newif\ifvthree \vtwotrue \begin{document} \ifvone v1 true \fi \ifvtwo v2 true \fi \ifvthree v3 true \fi \end{document}


4

I had the same problem than you, I resolve it but I use Makefile. I think this is the prettiest solution because you don’t need to modify your document just before compiling. For example, this is the code I use for compiling “offensive version of document” (In my publications, I could use swearwords but in nooffensive version they didn’t appear). Makefile ...


4

Here is an example of what you could use with the requirements you specify: \documentclass{article} \usepackage{environ} % Conditions: % 1) If \generateversion is absent and there is no \begin{version}: do as usual; % - Met by default, as text will be set as usual. % 2) If \generateversion is absent and there is at least one \begin{version}: take only one ...


4

As Heiko pointed out in his comment, \textbf is eating \else as argument, and \textit takes \fi. You thus need \expandafter in order to tell TeX to wait and finish the if-statement before expanding \textbf and \textit. You should then use \ifnum1=1\relax\expandafter\textbf\else\expandafter\textit\fi{Foo} or \ifnum1=1 ...


4

Add \makeatletter \def\@classoptionslist{draft} \makeatother before the \documentclass line (while canceling out the explicit 'draft' option from your class declaration) in the main.tex to make sure that the draft option is active also in your subfile text.tex. The following one-liner is equivalent: ...


4

Simple LaTeX with the kernel command \in@: \documentclass{article} \makeatletter \newcommand*{\IfStringInList}[2]{% \in@{,#1,}{,#2,}% \ifin@ \expandafter\@firstoftwo \else \expandafter\@secondoftwo \fi } \makeatother \begin{document} \IfStringInList{Paul}{George,John,Paul,Ringo}{Beat it}{Roll it} \end{document} Manual solution without ...


3

expl3 has this ready for use: \clist_if_in:nnTF {<clist>} {<item>} {<true>} {<false>}: \documentclass{article} \usepackage{expl3,xparse} \ExplSyntaxOn \NewDocumentCommand \IfStringInList {mmmm} { \clist_if_in:nnTF {#2} {#1} {#3} {#4} } \ExplSyntaxOff \begin{document} \IfStringInList{Paul}{George,John,Paul,Ringo}{Beat it}{Roll it}% ...


3

When TeX is given the input \ifx\\\\A\else B\fi it compares \\ with \\ and determines that they are equal; so it removes the conditional and the test tokens, leaving A\else B\fi This executes A (which will usually be some code) and then expands \else; the expansion of \else consists in going up to the matching \fi, removing everything it finds in ...


3

The reason is already explained by Joseph Wright's answer, that the second \index call is inside the argument of another macro. Then the the index entry text is not read verbatim and macros are expanded. Due to LaTeX's protection mechanism, the robust macro \textit is expanded by \protected@write to \protect\textit␣. The space at the end is part of the macro ...


3

The xtring package provides such command: \documentclass{article} \usepackage{xstring} \newcommand\IfStringInList[2]{\IfSubStr{,#2,}{,#1,}} \begin{document} \IfStringInList{Paul}{George,John,Paul,Ringo}{True}{False} \IfStringInList{Joe}{George,John,Paul,Ringo}{True}{False} \IfStringInList{ul,Ri}{George,John,Paul,Ringo}{True}{False} \end{document}


3

Wouldn't you prefer to test \foo directly, rather than through the intermediate definition of \test? \documentclass[11pt]{article} \def\foo#1{\relax} \let\svfoo\foo \def\testfoo{\ifx\svfoo\foo true\else false\fi} \begin{document} \testfoo \renewcommand{\foo}[1]{{\bfseries #1}} \testfoo \renewcommand{\foo}[1]{{\ttfamily #1}} \testfoo ...


3

Instead of opening the environment, use the true/false arguments to define a macro: \documentclass{article} \usepackage{ifthen} \newenvironment{selectlist}[1] {% \ifthenelse{\equal{#1}{itemize}} {\def\selectedlist{itemize}} {\def\selectedlist{enumerate}}% \begin{\selectedlist}% } {\end{\selectedlist}} \begin{document} ...


3

This syntax relies on the fact that one will use braces to enclose arguments (otherwise the question is ambiguous since any subsequent token can be interpreted as a second argument). \documentclass{article} \makeatletter \def\mycommand#1{\@ifnextchar\bgroup{\mycommandhelp{#1}}{\mycommandhelp{#1}{#1}}} \makeatother \def\mycommandhelp#1#2{Mycommands ...


3

Here's a xparse solution using the g specifier as a possible optional 2nd (!!!) argument, the g specifier allows for {} delimited optional arguments, but in my point of view, [] would be a clearer way. \documentclass{article} \usepackage{xparse} \NewDocumentCommand{\xparsecmd}{mg}{% \IfValueTF{#2}{% optional #1 and #2 }{% Only #1 and #1 }% ...


3

Usually this is realized in the form \foo{Unique} and \foo[One]{Two} which is clearer than an optional argument in braces. The classical LaTeX way to do this is \newcommand{\foo}{\@dblarg\name@foo} \def\name@foo[#1]#2{Whatever we want to do with #1 and #2} So calling \foo{X} will result in Whatever we want to do with X and X whereas calling ...


2

Just in case, here is a Plain solution with optional braced 2nd argument: \def\foo#1{\def\tmp{#1}\afterassignment\fooaux\let\next= } \def\fooaux{% \ifx\next\bgroup \fooprocess\next \else \fooprocess\tmp\next \fi } \def\fooprocess#1{Something with \tmp\ and #1} \foo{bar} \foo{bar}{baz} \bye with optional bracket 1st argument: ...


2

For macro-comparison, use \ifx, not \if: \documentclass{article} \makeatletter \newcommand{\@srulesI}{} \newcommand{\@srulesII}{} \newcommand{\srules}[2]{ \renewcommand\@srulesI{#1} \renewcommand\@srulesII{#2}}%%%%% \newcommand{\@emptymacro}{} \makeatother \begin{document} \srules{asdf}{\rule{0.5in}{0.5in}}%%%%% \makeatletter ...


2

As you ask for a modular solution, a simple but safer approach, without deal with complex conditional or string comparisons, is maintain each set of questions in separate files (said A.tex, B.tex, C.tex... each with a single line of text as "This is the version A", etc. ) Then you can insert the A.tex subdocument in the main document with \include{A} and ...


2

The \index command reads its argument in a verbatim-like manner. That means that it cannot be used inside the argument to other commands, at least not if you want it to continue to work correctly. If you look at the .idx file you have you'll find \indexentry{myterm@\textit{myterm}}{1} \indexentry{myterm@\textit {myterm}}{1} where the entry with the ...


2

Some macros are robust, while others are fragile. Robust macros protect themselves from expansion under certain conditions while fragile commands do not, and this is the cause of your current problem. Specific to your case, \small is not robust, and when used inside an expanded definition runs into problems. Instead, use \makeatletter ...


1

Another way... EDITED to allow blank fields (A,,B) \documentclass{article} \usepackage{ifthen} \newcommand\IfStringInList[4]{\stringsearch#1:#2,\relax\relax% \if T\found#3\else#4\fi} \def\stringsearch#1:#2,#3\relax{\ifthenelse{\equal{#1}{#2}}{\def\found{T}}{% \if\relax#3\relax\def\found{F}\else\stringsearch #1:#3\relax\fi}} \begin{document} ...


1

Here's a LuaLaTeX-based solution. It sets up a macro called \IfStringInList and implements it via a call to the Lua function string.find to set up "true" and "false" branches. Observe that both the target string and the search string can be quite general; in particular, the target string need not be a comma-separated collection of strings. % !TEX ...



Only top voted, non community-wiki answers of a minimum length are eligible