2

I am trying to calculate some width in a macro and use the macro for scalebox. However I get a flood of errors when I try to pass the macro to scalebox.

MinimalExample:

\documentclass{article}

\usepackage{pgf}

\newcounter{Counter}
\setcounter{Counter}{1}

\newcommand{\testOk}{1}
\newcommand{\testFail}{
 \theCounter
 \stepcounter{Counter}
}


\setlength\parindent{0pt}
\begin{document}
  \testFail %ok
  \scalebox{\testFail}{a} %ok
  \scalebox{\testFail}{a} %fails
\end{document}

Update

Since I want to actually use scalebox transparently in a new environment I decided to try something along the lines pointed at by Steven B. Segletes. Redefining the macro to do first the macroexpansion and then call the underlying macro with the value. I tried:

\newenvironment{imageRow}{
 \begingroup\imageRow@reset
 \LetLtxMacro{\oldScalebox}{\scalebox}
 \renewcommand{\scalebox}[2]{
    \edef\tmpValue{##1}
    \oldScalebox{\tmpValue}{##2}
 }
}{
 \imageRow@complete\endgroup
}

However I keep getting 'missing endcsname' errors. (And a flood following that).

If it matters here is the function I am currently trying to pass as first argument:

\newcommand{\getIt}{%
    \stepcounter{imageRow@getCount}%
    %   
    \expandafter\ifx\csname imageRow@scale@Image@\theimageRow@rowCount @\alph{imageRow@getCount}\endcsname\relax
        1%
        \PackageWarning{imageRow}{Return to get correct scaling factors.}%
    \else
        \csname imageRow@scale@Image@\theimageRow@rowCount @\alph{imageRow@getCount}\endcsname\relax
    \fi
}
15
  • \scalebox needs a number but you have passed it instructions to typeset a counter and then do some arithmetic to add 1 and assign that value to a counter, that is never going to work, you need to do the arithmetic fist then pass in the value. Nov 5 '14 at 16:33
  • @DavidCarlisle: I am trying to write a small package to solve my old problem tex.stackexchange.com/a/125722/19326. The idea is to automatically do some calculation and retireve the result to pass it to a scaling package like scalebox or adjustbox. I need csname for the retrival.
    – ted
    Nov 5 '14 at 16:40
  • yes but as Steven shows below you need to pass the result not instructions to execute some arithmetic and assignments. internally it is count@=#1 which is fine if #1 is a number but if you pass it \the\mycount\relax \advance\mycount by 1 then it doesn't work. Nov 5 '14 at 16:44
  • Telling what you'd like to achieve would be a big step towards a solution.
    – egreg
    Nov 5 '14 at 19:00
  • I think your update suffers the same as your original. That is, argument #1 has more than just a number... it has operations. Using the \edef in your revised \scalebox will not remedy that. Therefore, you still end up passing operations to \oldscalebox. Nov 5 '14 at 20:31
1

Introduce \tfscalebox, so that the counter indexing occurs outside of the \scalebox argument.

\documentclass{article}

\usepackage{pgf}

\newcounter{Counter}
\setcounter{Counter}{1}

\newcommand{\testOk}{1}

\newcommand\tfscalebox[1]{\scalebox{\arabic{Counter}}{#1}\stepcounter{Counter}}

\setlength\parindent{0pt}
\begin{document}
  \tfscalebox{a} %ok
  \tfscalebox{a} %ok
  \tfscalebox{a} %ok
\end{document}

enter image description here

If you want the macro to perform a test before deciding to either output the \scalebox or increment Counter, then you could always pass a secondary argument to the macro on which to test. Below, I do it as an optional argument [F] to decrement the scale:

\documentclass{article}
\usepackage{pgf}
\newcounter{Counter}
\setcounter{Counter}{1}
\newcommand\tfscalebox[2][T]{\scalebox{\arabic{Counter}}{#2}%
  \if#1F\addtocounter{Counter}{-1}\else\stepcounter{Counter}\fi%
  \if0\arabic{Counter}\stepcounter{Counter}\fi}
\setlength\parindent{0pt}
\begin{document}
  \tfscalebox{a} %ok
  \tfscalebox{b} %ok
  \tfscalebox{c} %ok
  \tfscalebox[F]{d} %ok
  \tfscalebox[F]{e} %ok
  \tfscalebox{a} %ok
  \tfscalebox[F]{e} %ok
  \tfscalebox[F]{e} %ok
  \tfscalebox[F]{e} %ok
  \tfscalebox[F]{e} %ok
\end{document}

enter image description here

1
  • WHile this is going the right way, I updated my anwser (see update), basically I try to go with your solution of evaluating my function and then passing the value to scalebox.
    – ted
    Nov 5 '14 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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