I want to write different formulas related vectors like subtraction, addition in latex, but don't know how to do this as I am new to Latex. I searched a lot but did not find any useful result.
[Tex/LaTex] Vector Formulas Representation in Latex
vector
Related Solutions
You can use Andrew Stacey's tried and tested tikzmark
idea, first posted here
Adding a large brace next to a body of text
and repeated in many examples since.
The idea is to set a node
at each of the places in your table, and then connect the two nodes afterwards (using overlay
and remember picture
). You can tweak the placement of the connections using the calc
tikz library.
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newcommand{\tikzmark}[1]{\tikz[overlay,remember picture] \node (#1){};}
\begin{document}
\begin{table}
\begin{tabular}[t]{ll}
\textbf{Something here} & \\ \hline
A\tikzmark{firstmarker} & F \\
B & A\tikzmark{secondmarker} \\
C & G \\
D & H \\
E & I%
\end{tabular}
\end{table}
\tikz[overlay,remember picture]\draw[red,->] ($(firstmarker)+(.2em,0.5em)$)--($(secondmarker)+(-.6em,0.5em)$);
\end{document}
[Tex/LaTex] Writing a macro in LaTeX for getting a vector i,j,k representation by passing parameters
Here is one way you can do this:
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 theetoolbox
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. Thepgf
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}
Best Answer
You can decorate a variable, say x, with a vector arrow using
\vec{x}
in math mode.For a slightly more flexible definition (if you want, say, to have a vector whose name is more than one letter), you can put the following in the preamble of your document:
Then you can use, say,
\vec{AB}
, and the arrow will stretch to cover both.