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.

I could not find a native command in TikZ allowing to draw a line going through two specified points where an offset can be controlled. So far, I'm using the calc extension to perform what I need but there may be something better:

\documentclass[12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (V1) at (0.5,2.25); % initial point 1
\coordinate (V2) at (1.5,3.25); % initial point 2
\coordinate (V3) at ($1.2*(V1)-0.2*(V2)$); % offset 1
\coordinate (V4) at ($-0.2*(V1)+1.2*(V2)$); % offset 2
\draw (V3)--(V4); % line with offset
\end{tikzpicture}
\end{document}
share|improve this question

3 Answers 3

up vote 8 down vote accepted

It's defined in tkz-base. You need to give % of the line added at the right and at the left. It's useful to get 2*AB; 3*AB etc.

\documentclass[12pt]{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}

\tikzset{%
    add/.style args={#1 and #2}{
        to path={%
 ($(\tikztostart)!-#1!(\tikztotarget)$)--($(\tikztotarget)!-#2!(\tikztostart)$)%
  \tikztonodes},add/.default={.2 and .2}}
}  

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);
\node at (A) {$\bullet$};
\node at (B) {$\bullet$};
\draw [add= 1 and .5, red, ultra thick] (A) to (B);
\end{tikzpicture}   

\end{document} 

enter image description here

Application

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (1,0);
\foreach \t in {-0.25,0,...,4}
{\draw [add= {\t} and {\t}, 
       red,
       ultra thick] ([yshift=1.5*\t cm]A) to  ([yshift=1.5*\t cm]B) ;}
\end{tikzpicture}

enter image description here

share|improve this answer

If all you want to do is to extend the line in both directions you can use shorten >=<length> and shorten <=<length> and provide a negative length:

\draw [shorten >= -0.25cm, shorten <=-0.30cm] (V1)--(V2);

To be able to see this, I have defined new coordinate (V1') and (V2') which are offset horizontally from the original. The black line is your manually extended line from (V1) to (V2) (ie, (V3)--(V4)), and the red is a line from (V1') -- (V2') extended using the shorten specification. The location of each these coordinate is shown by the blue circle.

enter image description here

Code:

\documentclass[12pt]{book}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{tikz}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\coordinate (V1) at (0.5,2.25); % initial point 1
\coordinate (V2) at (1.5,3.25); % initial point 2
\coordinate (V3) at ($1.2*(V1)-0.2*(V2)$); % offset 1
\coordinate (V4) at ($-0.2*(V1)+1.2*(V2)$); % offset 2

\draw [black, thick] (V3)--(V4); % line with offset (manually extended)


%% Shift the coordinate to be able to see the result
\coordinate (V1') at ($(V1) + (0.50cm,0)$);
\coordinate (V2') at ($(V2) + (0.50cm,0)$);

%% Draw line but extend it in either direction (extended via shorten)
\draw [red, thick, shorten >= -0.25cm, shorten <=-0.30cm] (V1')--(V2');

%% Show the location of the coordinates -- useful for debugging
\draw [thin, gray,-latex]  (V1) -- (V1');
\draw [thin, gray,-latex]  (V2) -- (V2') ;
\foreach \name/\placement in {V1/left, V2/left, V3/left, V4/left, V1'/right, V2'/right} {%
    \node [fill=blue!50,shape=circle,inner sep=1pt] at (\name) {};
    \node [gray, \placement] at (\name) {\tiny $\name$};
}
\end{tikzpicture}
\end{document}
share|improve this answer
    
But how determine the values uses by shorten function of V1 and V2 ? –  Alain Matthes Mar 17 '12 at 20:43
    
@Altermundus: Not clear in question how it was determined to extend the line by 20%, so am assuming the length to extend could be determined by a similar process, what ever that is. –  Peter Grill Mar 17 '12 at 20:55
    
($1.2*(V1)-0.2*(V2)$) determines a % of the line but you are right, it's in the MWE and it's not in the question. –  Alain Matthes Mar 17 '12 at 21:13
    
Woudn't it be clearer to write \coordinate (V4) at ($(V1)!1.2!(V2)$); –  Olivier Mar 6 '13 at 15:03
    
@Olivier: The coordinate (V4) was given in the original question, and I did not modify that. I used the existing definitions of (V1) and (V2) to define (V1') and (V2') and then draw a line between (V1') and (V2') and extended the lines past these two coordinates. –  Peter Grill Mar 6 '13 at 18:30

Another solution using [pos =] in the path.

\begin{tikzpicture}
\coordinate (A) at (0,0);
\coordinate (B) at (4,2);
\path (A) -- (B) coordinate[pos=-1](dd) coordinate[pos=1.5](ff);
\draw (dd) -- (A)node{$\bullet$}-- (B) node {$\bullet$}--(ff);
\end{tikzpicture}

ligne

share|improve this answer
    
Definitely the easiest solution in this thread, thank you. –  henry Sep 16 at 14:03

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.