Hot answers tagged

27

A random symbol is taken from a list and appended to a token list; when the width of the accumulated symbols is more than the width of the word minus 2pt, the symbols are printed, otherwise another symbol is appended. \documentclass{article} \usepackage{xparse,pgf} \ExplSyntaxOn \NewDocumentCommand{\censor}{m} { \pointer_censor:n { #1 } } \seq_new:N \...


18

A humble attempt with lualatex: The replacement code is very naive, we would need something more robust. \documentclass{article} \directlua{ % my list of bad words bad_words = { "fish", "cat", "dog", "horse", "alligator" } % the replacement string replacement = "duck" % a replacement function which returns % both the altered line and the number % of ...


15

\ref can't go in \edef no matter whether you load hyperref or not. The code \documentclass{article} \begin{document} \section{Section}\label{first} \edef\temp{\ref{first}}\noindent Section = \temp \end{document} stops with ! Missing control sequence inserted. <inserted text> \inaccessible l.5 ...\temp{\ref{first}}\noindent ...


14

No, you don't need xstring for that. TeX's syntax is sufficient. \def in TeX use a parameter template to extract any format of argument. The syntax is (c.f. TeX for the Impatient): \def \foo parameter text {replacement text} Thus you can directly use \def\parsedate #1-#2-#3\stopmark{Use #1 and #2 and #3 as you wish} to get the different arguments in a ...


14

Does not bm just do what you want without any further redefinitions? \documentclass{article} \usepackage{bm,upgreek} \begin{document} \begin{equation} \psi, \uppsi, \Psi,\bm{\psi},\bm{\uppsi},\bm{\Psi} \end{equation} \end{document}


14

The answer is pretty simple: you can't. The instruction \edef works purely by expansion; no assignment can be performed during an \edef: when TeX finds an unexpandable token in \edef it passes over it and proceeds with the next token. So you can't say \newcount\baz \edef\foo{\advance\baz 1 \number\baz} and hope that \foo expands to 1. It will expand to \...


13

Leaving aside the pros and cons of how and why to do this, I find it a nice little exercise to do with xstring. So here's my take on it: \documentclass{article} \usepackage{xstring} \def\grawlix{{\makeatletter@\makeatother}\textdollar{$\sharp$}*?!} \newcommand{\censor}[1]{\StrLen{#1}[\result]% \expandarg\StrLeft{\grawlix}{\result}[]} \begin{document} ...


12

A plain TeX solution without additional packages, based on ideas of Kees van der Laan. \documentclass{article} \begin{document} \newcount\cur \newcount\cura \def\fifo#1{\ifx\ofif#1\ofif\fi \process#1\fifo} \def\ofif#1\fifo{\fi} \def\mynum#1{\cur\the\lccode`#1\relax\the\cur } \def\process#1{\cur\the\lccode`#1\relax\cura\cur \divide\cura by7 \multiply\...


12

One approach is using the optional argument of StrBefore: \documentclass{book} \usepackage{xstring} \usepackage{ifthen} \newcounter{try} \newcommand{\foo}[1]{\StrBefore{#1}{.}[\result]} \newcommand\test[1]{\foo{#1}\setcounter{try}{\result}\arabic{try}}% \begin{document} Result of foo\{3.2\}: \foo{3.2} Result of test\{3.2\}: \test{3.2} \end{document}


12

\documentclass{article} \makeatletter \newcommand{\replaceBS}[2]{{\escapechar=`/ \xdef#2{\expandafter\zap@space\detokenize\expandafter{#1} \@empty}}} \makeatother \begin{document} \def\WindowsFilename{c:\files\examples\pictures\myfile.png} \replaceBS{\WindowsFilename}{\TeXFilename} Current result: \texttt{\TeXFilename} Expected result: \texttt{c:/...


12

\documentclass{beamer} \setbeamertemplate{navigation symbols}{} \begin{document} \Huge \let~\pause \frame{ P~y~t~h~a~g~o~r~e~a~n~\ $a^{~2}~+~b^{~2}~=~c^{~2}$ } \end{document} Another approach \documentclass{beamer} \setbeamertemplate{navigation symbols}{} \begin{document} \makeatletter \def\BB{\pgfsys@begininvisible} \def\EE{\pgfsys@endinvisible} \...


11

The internal macro that checks the cases is \@xs@testcase. Using \tracingmacros=1 with your original input, we find: \@xs@testcase #1#2#3\@xs@nil ->\@xs@reserved@E {\CaseVar }{#1}{#2}{\@xs@ifempty {#3}{Oppsss, unknown case '\CaseVar '.}{\@xs@testcase #3\@xs@nil }} #1<-case A #2<-Found Case A #3<- {case B}{Found Case B} for the first test; you ...


11

The problem is, that your \temp in the tried fix is read as one token and not parsed correctly. Inserting a few \expandafters fixes this: \documentclass{article} \usepackage{amsmath} \usepackage{xparse} \usepackage{xstring} \NewDocumentCommand{\Int}{ >{\SplitList{;}}o}{% \IfValueT{#1}{\ProcessList{#1}{\IntLimitONE}}\,% } \NewDocumentCommand{\...


11

% File name: DoctorStrange.tex \documentclass{article} \usepackage{xstring} \begin{document} \StrBehind*{\jobname}{Doctor}% <---- Note the starred variant \end{document} gives Certain TeX commands like \jobname (as is in your case) expand into strings made of characters with catcodes 12 and 10, whereas ordinary letters from the alphabet is given ...


10

A macro can be defined with the prefix \long (this is done by default in \newcommand) and so \par is allowed in its arguments (if any). Otherwise \par is not accepted and throws an error. In particular \IfStrEq is built around \@xs@IfStrEq@@ which the package author decided not to make \long. I would be very cautious in using \IfStrEq with something read ...


10

The xstring package offers extra (read: advanced) options to control the expansion of the arguments. This is really not meant for beginners and a good understanding of how arguments are expanded is more than welcome. In your case the code can be tweaked to compile: \documentclass{article} \usepackage{xstring} \newcommand\checkempty[1]{ \...


10

The redefined \texttt is not robust. This can be done by using \DeclareRobustCommand and using package letltxmacro that takes care of LaTeX's internals of the original robust \texttt: \documentclass{article} \usepackage{xstring} \usepackage{letltxmacro} \LetLtxMacro\OldTexttt\texttt \DeclareRobustCommand*{\texttt}[1]{% \OldTexttt{\hspace{0.4em}#1}% ...


10

You might want to take a look at your .idx file to see that 'works' here is not quite what you are expecting: you'll see \indexentry{\StrBehind{Foo: Bar}{:}}{1} \indexentry{\let \reserved@d =*\def \par }{1} The reason is that \index reads it's argument verbatim. That's fine when it is not inside another command, but if you've already grabbed the argument ...


10

Another approach would be to parse it yourself, without needing any assignments (except in the preamble to define \foo and a helper macro): \documentclass{book} %\usepackage{xstring} %\newcommand{\foo}[1]{\StrBefore{#1}{.}} \makeatletter \def\fooex#1.#2\@nil{#1} \newcommand{\foo}[1]{\fooex #1.\@nil} \makeatother \newcounter{try} \newcommand\test[1]{\...


10

The \jobname primitive is a TeX string, so all characters have category code 12 (other) not 11 (letter) (with the exception of any spaces, which are category code 10 (space)). Slightly confusingly, xstring is carrying out a token-based comparison here not a string one! Assuming e-TeX is available you can fix your code easily \documentclass{article} \...


10

With listings you can redefine the numberstyle: \documentclass{article} \usepackage{listings} \lstset{ language=tex, basicstyle=\footnotesize\ttfamily\selectfont, keepspaces=true, numbers=left, numbersep=5pt, numberstyle=\numberwithprompt, } \newcommand{\lstprompt}{>>>} \newcommand\numberwithprompt[1]{\footnotesize\ttfamily\selectfont#...


10

The outer part is good. For deciding whether * appears, I use \SplitArgument for at most one occurrence of *; this will produce two arguments, the second of which is -NoValue- in case no asterisk appears. The rest is just splitting at the comma. \documentclass{article} \usepackage{amsmath} \usepackage{xparse} \NewDocumentCommand{\Int}{>{\SplitList{;}}...


9

For completeness, I shall mention that LaTeX3 makes use of the \pdfstrcmp primitive (or equivalents for XeTeX and LuaTeX) to compare strings expandably. Note that xstring has a weird definition of what a string is (which I don't want to investigate now), so if you are using advanced features, it may not be possible to directly translate to LaTeX3 lingo. \...


9

You can first replace the space with a special marker, say -\q_tobi_space_marker- so you can split at hyphens and have a way to check whether the item was a space. Here's an implementation. Plan of attack: We change spaces in the way outlined before The two lists are split into sequences The "syllables" sequence is mapped item by item, stepping a counter ...


9

Package xstring requires the e-TeX extensions. Otherwise you get the error, that \numexpr from e-TeX is undefined as seen in the question. It seems that PCTeX has some support for e-TeX, but it must be enabled. This is done during format generation, if the first characters is a star *. "How do I fix a Package etex error?" from PCTeX's home page describes ...


9

By default xstring doesn't look into groups. But you can force it to consider also the text in braces: \documentclass{article} \usepackage{xstring} \begin{document} \noexpandarg \StrSubstitute{John {knows} that Mary is a unicorn}{knows}{believes} \exploregroups \StrSubstitute{John {knows} that Mary is a unicorn}{knows}{believes} \StrSubstitute{John \...


8

Notes: Wikipedia escapes spaces with an underscore (_) Links with space do work, too. Nested macros and hyperlink problem, Martin Scharrer: The correct syntax is \hyperlink{<name>}{<text>} where <name> must be a valid, expandable label name. It can not contain any non-expandable commands. […] The name must instead expand directly to ...


8

The more general solution to including git metadata in documents is the gitinfo2 package (which supersedes gitinfo that I mentioned in a comment). Having set that up, \gitAbbrevHash gives you a seven character hash. There may be more elegant ways of achieving this, but as a workaround you can use the catchfile package to store the full hash in a macro, and ...


8

You have defined it as a command taking no arguments but used #1 in the definition so get an error, just as you would with \newcommand or \def. To pass the argument of the outer command on to \pfc you need to define \pfc as a macro taking one argument and then use \pfc{#1} in your second definition, to pass that argument on. So \NewDocumentCommand{\pfc}{m}...


8

David's answer is precious, efficient and nice for the problem at hand. However, it doesn't deal with the error you get. The package xstring has three modes of operations: \fullexpandarg \expandarg \noexpandarg and the first is the default. They are described in section 3.1.1 of the manual, together with the list of what arguments of the provided commands ...


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