Hot answers tagged

31

This answer has four five six four routines: the last update: leaner code 6, as I understand better my hacky use of \pdfescapestring and could remove some superfluous extras (if they had been really needed, these extras would not have been enough anyhow). the very last update: improved sub-routine for merging in code 6 bringing at least a 2x speed ...


13

There are numerous ways to do this, but the simplest given what you've described is to use the pgffor package which provides a simple syntax for such loops: \documentclass{article} \usepackage{pgffor} % next code just generates some files to mimic yours \begin{filecontents}{test-table-1.tex} \begin{table}[htpb] \begin{tabular}{ccc} A & B & C\\ A ...


13

You can use the odsfile package, which supports manipulations with OpenDocument Spreadsheet files. You need to convert your excel file to ODS using LibreOffice (Excel can do this as well, but LibreOffice produces cleaner files). Odsfile provides \includespread command with keyval interface: \documentclass{article} \usepackage{fontspec} \usepackage{odsfile} ...


12

With no package, only classical TeX: \newcount\tmpnum \def\myscale#1#2{% \tmpnum=#1 \the\tmpnum \loop \ifnum\tmpnum<#2 \advance\tmpnum by1 \quad\the\tmpnum \repeat } Using eTeX extension and axpandable solution: \def\myscale#1#2{#1% \ifnum#1<#2 \space ...


11

No pgffor package is required for this; just write your table preamble as: \begin{tabular}{l *{6}{n{2}{3}}} The general syntax is: *{n}{column(s) pattern} where n is the number of repetitions, and the pattern can be any number of column specifiers, @{some code}, !{some code}, >{…}, <{…}. Some part of this syntax depends on the array package – ...


10

With no package (but requiring e-TeX), a fully expandable solution: \documentclass{article} \makeatletter \newcommand{\myscale}[2]{% \ifnum#1=#2 $#1$\expandafter\@gobble \else \expandafter\@firstofone \fi {\myscale@aux{#1}{#2}}% } \newcommand{\myscale@aux}[2]{% \ifnum#1>#2 \expandafter\@firstoftwo \else ...


10

You can use \@tfor. I provide also a better redefinition of the dot under according to your wish: \documentclass{article} \usepackage{graphicx} \let\d\relax \DeclareRobustCommand{\d}[1]{% \oalign{#1\cr\hidewidth\scalebox{0.5}{\textbullet}\hidewidth\cr}% } \makeatletter \newcommand{\ds}[1]{% \@tfor\next:=#1\do{\d{\next}}% } \makeatother ...


10

You could use the TikZ polygon shapes in a \foreach loop. Add color and styles as you like. Key points are regular polygon shape of the shapes.geometric library relative positioning of the positioning library remember the last node, using characters a, b, ... for node names using the south anchor for positioning and anchoring at the same base line ...


10

A short demo using l3sort: \documentclass{article} \usepackage{expl3,l3sort,xparse} \ExplSyntaxOn \cs_set:Npn \quicksort_fp:nnTF #1#2 { \fp_compare:nNnTF {#2} > {#1} } \DeclareExpandableDocumentCommand \quicksort { m } { \tl_sort:nN {#1} \quicksort_fp:nnTF } \ExplSyntaxOff \edef\demo {% \quicksort {% ...


8

Update I simplified the code using a \pic for the fundamental domain. For the color, I present three options: Randomly selecting shades of a fixed color (red, in my example), gives different coloring for each piece: \documentclass[tikz]{standalone} \usetikzlibrary{calc} \tikzset{ fundamental/.pic={ \draw[,scale=0.4,black,fill=red!\tmp,rotate ...


8

One possibility: \documentclass[a4paper,10pt,landscape]{article} \usepackage{tikz} \begin{document} \begin{tikzpicture} \foreach [count=\i] \j in {a,b,...,j}{ \node (\i) at (\i, \i) {\j} ; } \end{tikzpicture} \end{document} percusse mentions the alphalph package in a comment, and its \alphalph does exactly what you ...


8

The definitions of \B and \C are the problem: \def\B{\advance\cnt@a by-1} \def\C{\ifnum\cnt@a>0} TeX does not stop at the end of the numbers, but continue expanding until it finds a token, which is not a digit, e.g.: \B1234\relax will decrease \cnt@a by -11234, not by -1. Or \B\C Then the \ifnum is executed before the counter is decreased. Both ...


8

EDIT Unlike the original code, this does not produce a compilation error when first typeset. Nor does it rely on the titling package. I define \subtitle{} and \@subtitle to handle the subtitles. titlepage is used within a redefined \maketitle to create the actual title pages. Since we redefine \maketitle, we can issue \maketitle, \title etc. multiple ...


8

Here's a LuaLaTeX-based solution. The two arguments of \myscale can be any expressions that evaluate to numbers under TeX's and Lua's combined rules. If the macro's second argument is smaller than the first, nothing is printed. % !TEX TS-program = lualatex \documentclass{article} \newcommand{\myscale}[2]{\directlua{% for i = #1, #2 do tex.print(i) ...


7

Using \whiledo of package ifthen: \documentclass{article} \usepackage{ifthen} \newcounter{mycount} \newcommand{\myscale}[2]{ \setcounter{mycount}{#1} \whiledo{\value{mycount}<#2} {\arabic{mycount}, \stepcounter{mycount}}% \arabic{mycount}.} \begin{document} \myscale{1}{6}\par \myscale{-4}{4} ...


7

interesting question, you can do this : \documentclass{article} \usepackage{pgffor} \newcommand{\myscale}[2]{% \foreach \index in {#1,...,#2}{\index\space\space}% } \begin{document} \myscale{1}{6} \myscale{9}{17} \end{document}


7

Short answer: You are looking for Auto Multiple Choice (AMC). Long answer: You are looking for Auto Multiple Choice (AMC) that is a LaTeX package and a free GUI program for Linux. If you are a Window user that make regularly exams, it worth to install Linux only to have this program. AMC can make one exam to each student with randomly questions, so each ...


6

This algorithm uses the median of the first, middle and last element for the pivot. I tried to minimize conversions between text and numbers, assuming that \csname is faster than \strip@pt. (With all the \expandafters needed I wonder if this is really true.) The first step is to parse the array stored as a comma delimited string (in other words, a tikz ...


6

Here's an approach based on the LaTeX 3 syntax made available by expl3. I modified the table a bit to get it to fit. Otherwise, I just duplicated your example to 3 different files with 3 different captions. Obviously, geometry is optional. I just used it to make things fit for demonstration purposes. The preamble defines a new command with the following ...


6

Just for kicks, with expl3/xparse: \documentclass{article} \usepackage{xparse} \ExplSyntaxOn \cs_new_protected:Npn \alain_range:nnN #1#2#3 { \seq_clear:N #3 \int_set:Nn \l_tmpa_int { #1 } \int_set:Nn \l_tmpb_int { #2 } \int_while_do:nNnn \l_tmpa_int < \l_tmpb_int { \seq_push:NV #3 \l_tmpb_int \int_decr:N \l_tmpb_int } \seq_push:NV #3 ...


6

It is easy to program with xstring (with no unuseful test): \documentclass{extarticle} \usepackage{xstring} \newcommand\fios{hellow world} \newcommand\occurs[1]{% \ifhmode\par\fi\begingroup\expandarg \StrDel{#1}{ }[\tempstr]% \occuraux\endgroup } \newcommand\occuraux{% \unless\ifx\tempstr\empty ...


6

This is a Lua snippet with some CJKV characters in the strings. I've selected an opentype font from TeX Live, but it doesn't contain some diacritical letters (e.g. č, ř and š), let me hope it is sufficient as a demonstration of handling UTF-8 strings. We run lualatex mal-letters.tex. % lualatex mal-letters.tex \documentclass[a4paper]{article} ...


6

You can use the macro \scanargs \macro x1,y1 x2,y2 ... xn,yn; and then you can use the scanned arguments in your \macro in the form \x1, \x2, ... \x9, \y9, but \x{10}, \y{22} etc. I show the example using your example: \documentclass{article}\usepackage{tikz} \newcount\tmpnum \def\scanargs #1#2;{\let\tmp=#1\tmpnum=0 \scanargsA #2 {},{} } \def\scanargsA ...


6

This would be my first try, using a counter (no need of additional packages): \documentclass[a4paper,10pt,landscape]{article} \usepackage{tikz} \newcounter{cnti} \begin{document} \begin{tikzpicture} \foreach \i in {0,...,9}{ \setcounter{cnti}{\i}\addtocounter{cnti}{1} \node (i) at (\i, \i) {\alph{cnti}} ; } ...


6

Like this? \documentclass{article} \usepackage{pgffor} \usepackage{graphicx} \begin{document} \foreach \x in {a,b,c} { \begin{figure}[p] \includegraphics[scale=.5]{example-image-\x} \caption{So, three figures displayed, and this one has the number \x\ of the letter position (or iteration count)} % ie. 1 for a, 2 for b, 3 for c \end{figure} ...


6

\loop is a very simple macro and can not be nested at the same group level: \documentclass{article} \newcounter{stafflines} \newcommand{\musicstaff}{% \setcounter{stafflines}{0} ...


6

In the calculation of \pgfmathsetmacro{\theta}{360*\k/\N} 360*\k easily becomes bigger than what LaTeX can handle. Reversing the multiplication order, solves this: \pgfmathsetmacro{\theta}{360/\N*\k}


5

As a single path: \documentclass[tikz,border=5]{standalone} \begin{document} \tikz\draw (0,0) \foreach \i in {3,...,10} \foreach \j in {1,...,\i}{ -- ++(360/\i*\j:1) }; \end{document}


5

\documentclass[12pt]{article} \usepackage{forloop} \begin{document} \newcounter{x} \forloop{x}{0}{\value{x}<5}{\noindent value is \arabic{x}\\} \end{document} You can also print the numbers as strings (zero, one, two , ...), if you load the fmtcount package and instead of \arabic{x} you can use \numberstring{x}.


5

An implementation with expl3, where I define a \newdrawingcommand that takes as arguments a command name and the replacement text; optionally a command based on \foreach can be added, for greater flexibility. In the replacement text, the various points can be referred to by \x and \y; these macros are available only there (they won't clobber other existing ...



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