5

The LaTeX line requirements are too restrictive (coprime and mag < 6). Is there any package that lets me just draw a line between two points?

Basically

\line(a,b)(c,d)

or something similar

I went through all the trouble of calculating the points for my lines using the fp package and now I can't draw the lines I need?

I'm looking for a smooth line that uses the unit length and some way to set its thickness.

3
  • use package pict2e it hasn't such restrictions for the basic line macros of LateX
    – user2478
    Mar 16, 2012 at 6:02
  • A lot of packages can draw lines pstricks, pict2e, tikz. The only difficulty is to draw a line relatively to a page and not inside a picture, pspicture or tikzpicture. Perhaps, you can read some pages of the documentations ! Mar 16, 2012 at 13:06
  • I decided to use tikz completely which made it much easier. I just didn't want to have to recode some things but it turned out for the best...
    – Uiy
    Mar 16, 2012 at 13:09

6 Answers 6

9

enter image description here

\documentclass{article}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}
\draw (-3,-3) grid (3,3);
\draw (-2,2) -- (2,-3);
\end{tikzpicture}
\end{document}

Explanation

  1. Define a canvas. (-3,-3) is the bottom left corner and (3,3) is the top right corner.
  2. \draw (-2,2) -- (2,-3); draws a line from (-2,2) to (2,-3).
  3. Compile the code with pdflatex or xelatex or latex-dvips-ps2pdf.
  4. Done.

You can use \tikz{\draw (-2,2) -- (2,-3);} or \tikz\draw (-2,2) -- (2,-3); if you don't need tikzpicture.

Use the following to change the unit, put it anywhere before the \tikz or tikzpicture.

\tikzset
{
    x=2cm,% default value is 1cm.
    y=3cm,% default value is 1cm.
}

The last attempt:

\documentclass{article}
\usepackage{tikz}
\begin{document}
I want to draw a red line of thickness 1 mm, one unit in horizontal direction must be equal to 1.1 cm, and
one unit in vertical direction must be equal to 1.2 cm. Here is the line,
 \tikz[x=1.1cm,y=1.2cm]{\draw[line width=1mm,red](-1,1)--(1,-1);}, is it nice?
\end{document}

enter image description here

1
  • Nice answer. Maybe you can add the the canvas is defined automatically as the smallest bounding box, which equals the dimension of the grid in your example. So compared with LaTeXs picture environment or pstricks it’s not necessary to define the canvas size manually
    – Tobi
    Mar 16, 2012 at 13:04
6

Well without know more it is difficult to know exactly what you want to do. It depends on how you want to draw the line? What are the units of the coordinates?

Using tikz you can easily draw the lines. Here the units default to cm, if not specified. Note in the last example the units were specified as inches so that was used.

enter image description here

The macro \Line as defined below takes three parameters, with the first one being optional

  • 1 = the style of the line, and since it is optional it is specified within []. If it is not provided, the default style for a line is used (which is black line).

  • 2, #3 are coordinates in (x,y) style

There are numerous other options for line styles for which you should refer to the TiKZ/PGF manual.

References:

Code:

\documentclass{article}
\usepackage{tikz}
\newcommand*{\Line}[3][]{\tikz \draw[#1] #2 -- #3;}%
\begin{document}
    \Line{(0,0)}{(3,0)}%

    \Line[red, thick]{(0,0)}{(3,0)}%

    \Line[blue, thick, dotted]{(0,0)}{(1.00in,0in)}%
\end{document}

if you want to draw the different lines on the same coordinate system, just put them in one tikzpicture environment. Note how the blue dotted line begins at 3.2cm, just after where the black line ended at 3.0cm.

enter image description here

\documentclass{article}
\usepackage{tikz}
\newcommand*{\Line}[3][]{\draw[#1] #2 -- #3;}%
\begin{document}
\begin{tikzpicture}
    \Line{(0,0)}{(3,0)}%

    \Line[red, thick]{(0,-0.5)}{(3,-0.5)}%

    \Line[blue, thick, dotted]{(3.2cm,0)}{(1.5in,0in)}%
\end{tikzpicture}
\end{document}
3
  • Your line does not share the same origin each time. It gets it's own "Frame" so I cannot easily plot things relative to a central origin.
    – Uiy
    Mar 16, 2012 at 3:52
  • 2
    Again, as in you other question it is difficult to help you if you don't provide some sort of MWE. I know it is obvious to you, but won't be to others. Basically, show the problem instead of describing it. Mar 16, 2012 at 3:56
  • Ok, have added a version where you can plot things relative to a central origin. Mar 16, 2012 at 4:18
5

There are several ways to do this

1) Absolute position in the page. I mean that south left corner has (0,0) for coordinates and north east corner of the page has (21,29.7) for coordinates (a4paper). I want to draw a line from (a,b) to (c,d).

1a) with only one compilation, you can use eso-pic and tikz

\documentclass[a4paper]{scrartcl}
\usepackage{tikz,lipsum,eso-pic} 
\makeatletter
\def\myline{\pgfutil@ifnextchar[{\my@line}{\my@line[]}}%
\def\my@line[#1](#2)(#3){%
\AddToShipoutPicture{%
\setlength{\unitlength}{1cm}% 
\begin{picture}(0,0)% 
\put(0,0){\tikz[overlay] \draw[#1] (#2) -- (#3) ;}%
\end{picture}}}%
\begin{document}  
\lipsum[1]
\myline[magenta,thick](0,0)(21,29.7)
\myline[magenta,thick](0,29.7)(21,0)  
\myline[blue,ultra thick](10,10)(15,20)   
\myline[blue,ultra thick](10,20)(15,5) 
\end{document} 

enter image description here

1b) With two compilations, we need only tikz

\documentclass[a4paper]{scrartcl}  
\usepackage{tikz,lipsum} 
\makeatletter
\def\myline{\pgfutil@ifnextchar[{\my@line}{\my@line[]}}%
\def\my@line[#1](#2)(#3){%
\tikz[overlay,remember picture]%
\draw[#1] ([shift={(#2)}] current page.south west) -- 
          ([shift={(#3)}] current page.south west);   
}
\begin{document}  
\myline[magenta,thick](0,0)(21,29.7)
\myline[magenta,thick](0,29.7)(21,0)  
\myline[blue,ultra thick](10,10)(15,20)   
\myline[blue,ultra thick](10,20)(15,5)   
\end{document} 

2) position relative with one compilation

\documentclass[a4paper]{scrartcl}
\usepackage{tikz} 
\makeatletter
\def\myline{\pgfutil@ifnextchar[{\my@line}{\my@line[]}}%
\def\my@line[#1](#2)(#3){%
\tikz[overlay] \draw[#1]  (#2)--(#3); 
}%

\begin{document}  

In visions\myline[red](0,0)(3,-4) of the dark night\\
I have dreamed of joy departed-\\
But a waking dream of life and light\\
Hath left me broken-hearted.\\

\bigskip
Ah! what is not a dream by day\\
To him whose eyes are cast \\
On things around him with a ray \\
Turned back upon the past? \\
\end{document} 

enter image description here

3) between two points defined in the page (two compilations)

\documentclass[a4paper]{scrartcl}
\usepackage{tikz} 
\makeatletter
\def\myline{\pgfutil@ifnextchar[{\my@line}{\my@line[]}}%
\def\my@line[#1](#2)(#3){%
\tikz[remember picture,overlay] \draw[#1]  (#2)--(#3); 
}%
\newcommand\mypoint[1] {%
\tikz[remember picture] \path coordinate (#1);}% 

\begin{document}  
\parindent=0pt
\mypoint{a}In visions of the dark night\\
I have dreamed of joy departed-\\
But a waking dream of life and light\\
Hath left me broken-hearted.\\ 

\bigskip
Ah! what is not a dream by day\\
To him whose eyes are cast \\
On things around him with a ray \\
Turned back upon the past? \mypoint{b} \\
\myline[red](a)(b)
\end{document} 

enter image description here

4

At A Glance

enter image description here

\documentclass{article}
\usepackage{pstricks-add}

\begin{document}
\begin{pspicture}[showgrid=true](-3,-3)(3,3)
\psline(-2,2)(2,-3)
\end{pspicture}
\end{document}

Explanation:

  1. Define a canvas. (-3,-3) is the bottom left corner, (3,3) is the top right corner.
  2. \psline(-2,2)(2,-3) draws a line from (-2,2) to (2,-3).
  3. Compile the code with xelatex (simpler way) or with latex-dvips-ps2pdf (simple way).
  4. Done.

More Options

There are too many options that I cannot list here. Please read the manual from the official PSTricks site.

enter image description here

\documentclass{article}
\usepackage{pstricks-add}

\begin{document}
\begin{pspicture}[showgrid=true](-3,-3)(3,3)
\psline
    [
        linewidth=2pt,
        linestyle=dashed,%dotted, solid, etc
        linecolor=red,
        arrows=<->,%<-, ->, *->, etc
    ]
    (-2,2)(2,-3)
\end{pspicture}
\end{document}

To change unit, you can use the following, put it anywhere before pspicture environment.

\psset
{
    xunit=2cm,%default value is 1cm.
    yunit=3cm,%default value is 1cm.
}

Based on the OP comments below, here is the example:

\documentclass{article}
\usepackage{pstricks-add}

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=12pt

\psset
{   
    xunit=1.1cm,% the default value is  1cm.
    yunit=1.2cm,% the default value is 1cm.
}

% activate polar coordinate system
\SpecialCoor

\begin{document}
\begin{pspicture}[showgrid](-3,-1)(3,3)
% a horizontal line
\psline(-3,0)(3,0)
% a red line passing through (-1,-1) with length 3 unit at angle 30 degree w.r.t the horizontal axis.
\psline[linecolor=red,origin={-1,-1}](3;30)
% a blue line passing through (-2,2) and (3,-1).
\psline[linecolor=blue](-2,2)(3,-1)
\end{pspicture}
\end{document}

enter image description here

The following code snippets are for getting a single PDF output that only contains the figure in question tightly. You can then use it for your main document by using \includegraphics from graphicx package. This "algorithm" is a recommended way (by me) to manage your figures such that they can be reused for many documents without significant complexities.

\usepackage[active,tightpage]{preview}
\PreviewEnvironment{pspicture}
\PreviewBorder=12pt
5
  • Is there another way to do it directly without a special environoment? I saw this when searching google along with tikz stuff.
    – Uiy
    Mar 16, 2012 at 3:19
  • @Uiy: See my other answer (at the last sentence) here. Mar 16, 2012 at 3:31
  • 1
    @Uiy: In PSTricks we usually use pspicture because for general case, the canvas must be defined in advance. There are some cases where we can use PSTricks graphic macro without pspicture such as making nodes and connecting them with a line or curve. Mar 16, 2012 at 3:55
  • Well, I'm not trying to do anything too advanced. Just plot some horizontal and two sloped lines. I suppose it might be beneficial to use some advanced method though for future purposes.
    – Uiy
    Mar 16, 2012 at 3:55
  • @Uiy: Keep giving me comments for other things you want to get. Mar 16, 2012 at 4:21
2

It needn't any package,use the command \qbezier(a,b) ( (a+b)/2,(c+d)/2 ) (c,d) to draw a line from point (a,b) to point (c,d). \qbezier is generally used to draw curves, especially, if the 2nd parameter is the midpoint between the 1st and the 3rd, the curve becomes a straight line, for example:

\documentclass{article}
\begin{document}
  %\setlength{\unitlength}{1mm}
  \begin{picture}(100,100)
    \qbezier(0,0)(40,40)(80,80)       %A line from (0,0) to (80,80) 
    \qbezier(0,0)(10,6)(100,60)       %2nd point doesn't have to be the midpoint.
    {\linethickness{2pt}\qbezier(0,100)(1,99)(100,0) }%Just need to satisfy the linear equation    
  \end{picture}
\end{document}
0

xpicture is a not a very popular package, but Interesting since its documention is much lighter than the pstricks or tikz ones This is an example :

\documentclass{article}
\usepackage{xpicture}
\usepackage{lipsum}
\begin{document}

\setlength{\unitlength}{1cm}
  \lipsum[1]

\begin{xpicture}(-5,-1)(4,2)
  % \cartesiangrid(-5,-5)(5, 5)
  {\pictcolor{red}\xLINE(-3,5)(4,-2)}
  \polarreference\degreesangles
  {\pictcolor{green}\xLINE(3,-120)(2,90)}
\end{xpicture}
\lipsum[2]
\end{document}

xpicture test

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .