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.

What if I only want to extend the line 1cm in one direction but not the other? I found this on another post here from a year ago.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
 extended line/.style={shorten >=-#1,shorten <=-#1},
 extended line/.default=1cm]

 \node [dot=A] at (0,0) {};
 \node [dot=B] at (3,1) {};
 \node [dot=P] at (1.9,-1.6) {};

 \draw [extended line=0.5cm] (A) -- (B);
 \draw [extended line] ($(A)!(P)!(B)$) -- (P);

 \fill [red] ($(A)!(P)!(B)$) circle [radius=2pt];

 \end{tikzpicture}
 \end{document}
share|improve this question

3 Answers 3

up vote 8 down vote accepted

enter image description here

Your figure extends on both ends since extended line style has shorten <=-#1. If you remove that, then the line will not be extended on the other end. But I suggest that you add another style.

one end extended/.style={shorten >=-#1},
one end extended/.default=1cm,

in your options.

Here is the complete code and usage.

%\documentclass{article}
\documentclass[preview,border=5]{standalone}
\usepackage{tikz}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,label={#1},name=#1},
 extended line/.style={shorten >=-#1,shorten <=-#1},
 extended line/.default=1cm,
 one end extended/.style={shorten >=-#1},
 one end extended/.default=1cm,
 ]

 \node [dot=A] at (0,0) {};
 \node [dot=B] at (3,1) {};
 \node [dot=P] at (1.9,-1.6) {};

\draw [extended line=0.5cm] (A) -- (B);
\coordinate (P') at ($(A)!(P)!(B)$);
\draw [one end extended] (P') -- (P);

\fill [red] (P') circle [radius=2pt];

 \end{tikzpicture}
 \end{document}

Edit

I added P' as name of the coordinate ($(A)!(P)!(B)$) as suggested in comment to speed up compilation time.

share|improve this answer
    
Saving the projected point as a named node might speed up the compilation a bit. :-) –  cyanide-based food Mar 9 '13 at 0:33
1  
@GarbageCollector Done. I haven't timed this. But I wasn't able to see a noticeable difference in compilation time. :) –  hpesoj626 Mar 9 '13 at 0:42

In PSTricks, we can use nodesepA (only for A end), nodesepB (only for B end), or nodesep (for both A and B ends) to shorten or extend the line ends.

enter image description here

\documentclass[pstricks]{standalone}
\usepackage{pst-eucl}
\begin{document}
\begin{pspicture}(8,6)
    \pstGeonode(2,3){A}(6,5){B}(6,2){P}
    \pstProjection[PointName=none]{A}{B}{P}
    \psset{nodesep=-1}
    \pstLineAB{A}{B}
    \pstLineAB[nodesepB=0]{P}{P'}
\end{pspicture}
\end{document}
share|improve this answer

This is possible with tkz-euclide or tkz-base but here a solution with only tikz

\documentclass{scrartcl}
\usepackage{tikz}
\usetikzlibrary{calc}

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

\begin{document}
\begin{tikzpicture}[dot/.style={circle,inner sep=1pt,fill,name=#1}]

 \node [dot=A,label={below:$A$}] at (0,0) {};
 \node [dot=B,label={below:$B$}] at (3,1) {};
 \node [dot=P,label={right:$P$}] at (1.9,-1.6) {};

\draw [add=.5 and .5] (A) to (B);
\coordinate (P') at ($(A)!(P)!(B)$);
\draw [add=0 and .5] (P') to (P);

\fill [red] (P') circle [radius=2pt];

 \end{tikzpicture}
 \end{document}

enter image description here

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.