# How to use \textwidth (or any variable) in a picture environment

I don't understand why this doesn't work. I am getting a compile error. Am I missing something?

\documentclass[12pt,a4paper]{article}
\begin{document}
\begin{picture}(0,0)
\linethickness{1.5pt}
\put(0,0){\line(1,0){\textwidth}}
\end{picture}
\end{document}


The error is in \textwidth. When I put a decimal value (like 137) in, the example compiles correctly. The reason that I want to use \textwidth is obvious, I want a line aligned with the text.

Strangely enough, variable expansion does work in a TikZ environment.

-
The \line macro does expects a length without pt, so you could use \strippt\textwidth instead (with \strippt defined as \makeatletter\def\strippt{\strip@pt}\makeatother). Though, in this case it would be easier to just use \rule{\textwidth}{1.5pt}. – Qrrbrbirlbel Apr 19 '13 at 2:02
Wow! This is an overwhelming numbers of answers, and really fast! About my user name: I realized I only logged on (using my Google account) but no user name was created. Indeed, user1234 is dull. I changed it to my common name on forums. – jlinkels Apr 19 '13 at 15:49
I see what the problem is and why TikZ does accept it. I know I could use \rule, but this is only a MWE. – jlinkels Apr 19 '13 at 15:53

The picture environment expects factors for the length \unitlength. Package picture redefines the picture macros to detect length specifications to support them additionally:

\documentclass[12pt,a4paper]{article}

\usepackage{picture}

\begin{document}
\noindent
\begin{picture}(0,0)
\linethickness{1.5pt}
\put(0,0){\line(1,0){\textwidth}}
\end{picture}
\end{document}

-
This seems to be the really easiest solution. I will try it as well. Thanks – jlinkels Apr 19 '13 at 15:54

The \line macro expects a length without a unit as it uses \unitlength (default: 1pt) for this parameter (as well to the argument of the picture environment which size you have set to 0pt × 0pt by the way).

Here are three and a fourth possible solution to deal with that. The first one just stripes the pt from \textwidth so that the picture environment can multiply it again with the \unitlength of 1pt.

The second one is similar to the first in that it sets \unitlength to \textwidth so that the \line macro can be used as \line(1,0){1}.
The third one goes one step further and sets \unitlength to \textwidth for the whole picture so that even its dimension can be given in \textwidth lengths.

The fourth solution simply uses the LaTeX macro \rule to mimic the output of your picture environment.

## Code

\documentclass[12pt,a4paper]{article}
\usepackage[showframe,pass]{geometry}
\makeatletter
\def\strippt{\strip@pt}
\makeatother
\begin{document}
\noindent
\begin{picture}(\strippt\textwidth,0)%
\linethickness{1.5pt}%
\put(0,0){\line(1,0){\strippt\textwidth}}
\end{picture}

\noindent
\begin{picture}(\strippt\textwidth,0)%
\linethickness{1.5pt}%
\put(0,0){\setlength{\unitlength}{\textwidth}\line(1,0){1}}
\end{picture}

\noindent\begingroup\setlength{\unitlength}{\textwidth}%
\begin{picture}(1,0)%
\linethickness{1.5pt}%
\put(0,0){\line(1,0){1}}
\end{picture}
\endgroup

\noindent\rule[-.75pt]{\textwidth}{1.5pt}
\end{document}


## Output

-
Thanks so much! – jlinkels Apr 19 '13 at 15:54