\documentclass[a4paper,twocolumn]{article}
\usepackage{lipsum,graphicx}
\newcommand{\mydimright}{%
\dimexpr\paperwidth-1in-\hoffset-\oddsidemargin-
\columnwidth-\columnsep\relax}
\newcommand{\mydimleft}{%
\dimexpr1in+\hoffset+\oddsidemargin+\columnwidth\relax}
\begin{document}
\lipsum[1]
\begin{flushleft}
\makebox[\columnwidth][r]{%
\includegraphics[height=1cm,width=\mydimleft]{try}}
\end{flushleft}
\lipsum[1-5]
\begin{flushleft}
\makebox[\columnwidth][l]{%
\includegraphics[height=1cm,width=\mydimright]{try}}
\end{flushleft}
\end{document}
There is no immediate access to the right margin width, which has to be computed in an indirect way.
With the command \layout
of the layout package, one can look at the relevant lengths. I've put also \hoffset
, because some classes might change it.
Here try
is the graphic file I always use for tests. I've used flushleft
just in order to avoid LaTeX trying to be smart with figure placement.
-- Extended version for automatic placement in the figure environment --
Here it is, following Martin's suggestion; the command \touchmarginincludegraphics
works like its sibling \includegraphics
except, of course, that the keyword width
should not be specified.
\documentclass[a4paper,twocolumn]{article}
\usepackage{lipsum,graphicx}
\makeatletter
\def\tmig@r{\dimexpr\paperwidth-1in-\hoffset-
\oddsidemargin-\columnwidth-\columnsep\relax}
\def\tmig@l{\dimexpr1in+\hoffset+\oddsidemargin+\columnwidth\relax}
\newcommand{\touchmarginincludegraphics}[2][]{%
\begingroup
\if@firstcolumn
\def\tmig@place{r}%
\else
\def\tmig@place{l}%
\fi
\makebox[\columnwidth][\tmig@place]{%
\includegraphics[#1,width=\@nameuse{tmig@\tmig@place}]{#2}}
\endgroup}
\makeatother
\begin{document}
\lipsum[1]
\begin{figure}[!h]
\touchmarginincludegraphics[height=1cm]{try}
\end{figure}
\lipsum[1-4]
\begin{figure}[!h]
\touchmarginincludegraphics[height=1cm]{try}
\end{figure}
\lipsum[2]
\end{document}
\makebox[\columnwidth][r]{\includegraphics[width=\dimexpr\columnwidth+\rightmargin\relax]{image}}
in this case. I'm not sure about the margin length, though.\rightmargin
is definitely wrong; it's a parameter for lists.