Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How to do this using macros in TeX. Something like \power{n}{k} - the power of n to the value k - but without using \multiply.

Something like this needs to be done

k_{n+1} = n^2 + k_n^2 - k_{n-1}
share|improve this question
1  
It is unclear what your question means. Please provide some code demonstrating what you want to achieve. Do you want to typeset a formula or calculate something with a macro? –  Stephan Lehmke Apr 10 '12 at 17:20
    
A macros in TEX that compute The power nk, but without \multiply –  Andy Apr 10 '12 at 17:27
    
If you'd try to do it yourself (using \numexpr) and show where you get stuck, you'd give people here something to start from. Or else, explain a little more about the context you need this for. The way you ask, it looks like you're asking people here to do your homework for you. –  Stephan Lehmke Apr 10 '12 at 17:30
2  
Why without \multiply? –  Joseph Wright Apr 10 '12 at 17:43

2 Answers 2

Assuming you want to work with integers, and e-TeX is available, something like

\documentclass{article}
\newcommand*{\power}[2]{%
  \the\numexpr\poweraux{#1}{#2}\relax
}
\newcommand*{\poweraux}[2]{%
  \ifnum\numexpr#2 = 0 %
    \expandafter\powerend
  \else
    #1 * \expandafter\poweraux
  \fi
  {#1}{#2 - 1}%
}
\newcommand*{\powerend}[2]{1}
\begin{document}
\power{2}{3}
\end{document}

seems about right. I've not tested for edge cases or made this 'maximum efficiency' as I'm not entirely sure what is required.

share|improve this answer

Use the fp package (or pgfmath from the pgf bundle) to produce floating point calculations without having to manually specify \multiply, \divide, or the like. Here's a small example:

enter image description here

\documentclass{article}
\usepackage[nomessages]{fp}% http://ctan.org/pkg/fp
\begin{document}
\FPeval\result{round(3^4:0)}%
$3^4=​​​​​​​​​​​​​​​​​​​​​​​​​​​\result$
\end{document}​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

See the fp README for more information on the notation and usage.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.