13

I am kind of new to LaTeX. I am doing project which contain resources (formula, solutions to problems, e.g.) i can bring to the Exams day on 16th March. I have been looking for a solution to my problem couple of hours now. What i am trying to achieve is to create a \newcommand with indefinite variable (arguments?) that can do a repetitive task such as 5 \times 10 \times 15 \times 20 \times 25. The numbers are supposed to be the arguments and \times, the repeating task between each argument. I have tried to make a tabular many column inside a \newcommand but have not succeed. To give a better vision, i will post a piece of my code.

\documentclass{article} \usepackage{lmodern} \usepackage[T1]{fontenc} \usepackage[utf8]{inputenc} \usepackage{amsmath} \usepackage{enumitem} \usepackage{titlesec} \usepackage[norsk]{babel} \titleformat{\section}{\LARGE\filcenter}{}{1em}{} \setlength{\jot}{14pt} \title{Hjelpermidler i Digital Radio} \author{Sami Rashiti} \newcommand{\degree}[2]{#1\,^{\circ}\ \mathrm{#2}} \begin{document} \maketitle \newpage \section*{Innlevering 1} \input{innleveringer/innlevering1.tex} ~\newline \begin{align} e_n &= \sqrt{4KTBR} \\ e_n^2 &= 4KTBR \\ R &= \frac{e_n^2}{4KTB} \\ &= \frac{(\frac{V_o}{A})^2}{4KTB} \qquad ,der \quad e_n = \frac{V_o}{A} = 3,2\mu V \\ &= \frac{(3.2\mu V)^2}{4\cdot1.38\cdot 10^{-23}\cdot 310\cdot 100\cdot 10^3} \\ &= 6\ k\Omega \end{align} \end{document} 
3
  • 1
    You can use a list. Commented Mar 6, 2015 at 17:49
  • 1
    As an aside, check out the siunitx package for formatting your units. Commented Mar 6, 2015 at 17:50
  • Welcome to TeX.SX! A tip: If you indent lines by 4 spaces or enclose words in backticks `, they'll be marked as code, as can be seen in my edit. You can also highlight the code and click the "code" button (with "{}" on it). Commented Mar 6, 2015 at 17:56

4 Answers 4

13

You can use a list; here's an example using etoolbox and borrowing the idea for the first item in the list used by Werner in his answer to Iterating through comma-separated arguments:

\documentclass{article} \usepackage{etoolbox} \newcommand\Multiply[2][\cdot]{% \def\nextitem{\def\nextitem{#1}}% Separator \renewcommand*\do[1]{\nextitem##1}% How to process each item \docsvlist{#2}% Process list } \begin{document} \[ \Multiply[\times]{4,1.38\cdot 10^{-23},310,100,10^3}\qquad \Multiply{a,b,c,d,e,f,g,h,i,j,k,l,m,n} \] \end{document} 

enter image description here

1
11

It's a breeze with expl3. Separate the items by a comma and that's all.

\documentclass{article} \ExplSyntaxOn \NewExpandableDocumentCommand\Multiply{O{\cdot}m} { \clist_use:nn {#2} {#1} % put #1 between items } \ExplSyntaxOff \begin{document} $\Multiply[\times]{4,1.38\cdot 10^{-23},310,100,10^3}$ $\Multiply{a,b,c,d,e,f,g,h,i,j,k,l,m,n}$ \end{document} 

output

3
  • Note that, differently from the other answers, this method removes spaces around the items. So with this method, inputting \Multiply{ a , b} is equivalent to inputting \Multiply{a,b}. It's not relevant in this case, because the macro works in math mode, but it can be very important in case of a macro used in normal text. Commented Mar 9, 2015 at 8:55
  • \usepackage{xparse} has become useless. Commented Jul 8 at 8:34
  • 1
    @projetmbc Yes, indeed. I took the occasion for simplifying the code with a tool that wasn't available at the time. Commented Jul 8 at 8:49
6

The common method how to deal more arguments at TeX primitive level is:

\def\Multiply#1{\MultiplyA#1,,} \def\MultiplyA#1,{#1\MultiplyB} \def\MultiplyB#1,{\ifx,#1,\else\cdot#1\expandafter\MultiplyB\fi} $$ \Multiply{a,b,c,d,e,f,g,h,i,j,k,l,m,n} $$ \end 
1

A bad implementation. I did this because I want to type the s-expression of Scheme using MathJax, and MathJax only supports \def and \newcommand. The brackets are used to stop the recursion.

\newcommand{\fact}[2][% \times{}\fact ]{ #2 #1 } Here is the result of $\fact876[]5$ . 
1
  • As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center. Commented Jul 8 at 8:06

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.