[Tex/LaTex] Writing a macro in LaTeX for getting a vector i,j,k representation by passing parameters

macrosvector

I want to create a newcommand \vecty to create a vector which will take 3 parameters
like \vecty{1}{2}{3} and this should display as $\hat{i}+2\hat{j}+3\hat{k}$
and if I give \vecty{0}{3}{-3} then it should show $3\hat{j}-3\hat{k}.

I am using LaTeX. I would appreciate any guidance that comes how to get through it.
I did try \ifthenelse code but it turned to be quite hefty.
You can have a look at the code.

But I know that the code is too bad. Hence wanted to understand if there are other ways to implement it. Kindly help I m a newbie.

Best Answer

Here is one way you can do this:

enter image description here

Notes:

  • This updated version properly handles integer and decimal coefficients of zero (+0, 0.00) or one with leading + signs, spurious spaces, and non-numbers in the input (as per the second section of the table above).

  • newtoggle from the etoolbox package was used as I prefer that syntax versus the \newif syntax. But if you don't want to include an additional package it should be pretty straightforward to adapt this to use \newif or some other conditional methods. This toggle keeps track if a term has already been printed to ensure that a leading positive term does not have a +.

  • I used the xstring package to determine if the number had a leading minus sign, or was zero, but again, this could easily be adapted to not use that package as well.

  • I would recommend that you use this macro as $\vecty$ - that is you explicitly enter math mode, as used below. If however, you wish to use it without having to enter math mode as per your original question, you should not surround it with dollar signs, but instead use

    \ensuremath{\LeadingSign #1 #2}

    as then it will be useable inside or outside of math mode. To repeat, I am not recommending this as this is clearly a math mode macro. Please see When not to use \ensuremath for math macro? if you disagree.

Further Enhancements:

  • Should \vecty be required to typeset more than three components, one can easily add additional calls to \Display{}{} for each of the components -- no other changes should be required. Note that this has not been tested.
  • This does not remove the coefficient equal to one, nor eliminate the term if the coefficient is zero for the case of non numerical input: For example \vecty{-1x}{+0y}{1z}.
  • Could use pgf math to do numerical processing of the input so that basic expressions in the coefficients could be simplified. The pgf math functions could also be used to convert -0.5 to -\frac{1}{2}.

Code:

\documentclass{article}
\usepackage{amsmath}
\usepackage{xstring}
\usepackage{etoolbox}

\newcommand*{\LeadingSign}{}%
\newcommand*{\CleanedCoefficientWithNoSpaces}{}% coefficient with spaces & "+" removed
\newcommand*{\CleanedCoefficient}{}% digit "1" removed if coefficient is 1, or -1

\newcommand*{\Display}[2]{%
    % #1 = coefficient (may not be a number)
    % #2 = paramater
    %
    % ---------------------------------------------------------------- Clean input
    \StrSubstitute{#1}{ }{}[\CoefficientWithNoSpaces]% eliminate any spaces
    \IfBeginWith{\CoefficientWithNoSpaces}{+}{% eliminate any leading + sign
        \StrSubstitute{\CoefficientWithNoSpaces}{+}{}[\CleanedCoefficientWithNoSpaces]%
    }{%
        \renewcommand*{\CleanedCoefficientWithNoSpaces}{\CoefficientWithNoSpaces}%
    }%
    % ---------------------------------------------------------------- 1, +1, -1 issue
    % If the coefficient is 1, +1, or -1, we need to supress the digit
    \IfEq{\CleanedCoefficientWithNoSpaces}{1}{% coefficient equivalent to +1
        \renewcommand*{\CleanedCoefficient}{}%  eliminate the digit
    }{%
        \IfEq{\CoefficientWithNoSpaces}{-1}{% coefficient equivalent to -1
            \renewcommand*{\CleanedCoefficient}{-}% eliminate the digit (leave sign)
        }{%
            % This is the case of the coefficient not being +1, or -1, so use as is
            \renewcommand*{\CleanedCoefficient}{\CleanedCoefficientWithNoSpaces}% 
            %
            % The issue of the leading sign is dealt with below.  Could have moved the
            % leading sign handling here (with some adjustments above), but that would 
            % have made the code a little harder to read. There is already enough  
            % illiteracy in the world. :-)
        }%
    }%
    % ----------------------------------------------------------------  Leading sign
    % We don't want a + sign for the very first term printed.
    \renewcommand*{\LeadingSign}{}% initalize
    \IfBeginWith{\CleanedCoefficientWithNoSpaces}{-}{%
        % use default empty value (sign is part of number)
        % or for the case of -1, this has already been set above
    }{%
        \iftoggle{PrintedFirstTerm}{%
            \renewcommand*{\LeadingSign}{+}%
        }{%
            % Since a leading term of this vector has not yet been printed 
            % we do not add a + sign
        }%
    }%
    % ----------------------------------------------------------------  Print
    \IfEq{#1}{0}{}{%
        \LeadingSign \CleanedCoefficient #2%
        \global\toggletrue{PrintedFirstTerm}% so next term can have + sign
    }%
}%

\newtoggle{PrintedFirstTerm}
\newcommand*{\vecty}[3]{%
    \global\togglefalse{PrintedFirstTerm}%
    \Display{#1}{\hat{i}} %
    \Display{#2}{\hat{j}}%
    \Display{#3}{\hat{k}}%
    \iftoggle{PrintedFirstTerm}{}{\mathbf{0}}% could also use \vec{0} here
}%

\begin{document}
\[
\begin{array}{ll}
  \verb|$\vecty{1}{2}{3}$|        & \vecty{1}{2}{3}  \\
  \verb|$\vecty{0}{3}{-3}$|       & \vecty{0}{3}{-3} \\
  \verb|$\vecty{0}{0}{0}$|        & \vecty{0}{0}{0}  \\
  \verb|$\vecty{0}{0}{3}$|        & \vecty{0}{0}{3}  \\
  \verb|$\vecty{1}{2}{0}$|        & \vecty{1}{2}{0}  \\
  \verb|$\vecty{0}{3}{3}$|        & \vecty{0}{3}{3}  \\
  \verb|$\vecty{3}{0}{0}$|        & \vecty{3}{0}{0}  \\
  \verb|$\vecty{-1}{1}{0}$|       & \vecty{-1}{1}{0} \\[1.5ex]
  \verb|$\vecty{- 1}{ - 1}{+ 0}$| & \vecty{- 1}{ - 1}{+ 0}\\
  \verb|$\vecty{-1}{-1.0}{+2}$|   & \vecty{-1}{-1.0}{+2}\\
  \verb|$\vecty{-0.0}{+0}{0}$|    & \vecty{-0.0}{+0}{0}\\
  \verb|$\vecty{-0}{+x}{y}$|      & \vecty{-0}{+x}{y}\\
  \verb|$\vecty{-x}{+7}{y}$|      & \vecty{-x}{+7}{y}\\
\end{array}
\]
\end{document}
Related Question