5

I am trying to create a command that moves (short) keywords to the left side of the text. They should be "right" aligned" with a fixed separation between the keyword and the text.

The command below does what I want, but only if the text is indented or not - not with a combination, e.g. after the start of a new section.

Can I check whether the note is in an indented paragaph \hspace{\parindent} and issue conditionally? Or is there a better approach? I don't want to use the marginnote package (or the marginpar in general) for this.

\documentclass{scrartcl}

\newcommand{\parnote}[1]{%
\begingroup
\setlength{\fboxsep}{0pt}
    \hspace*{0pt}\llap{%
        \framebox[2cm][r]{% normally use makebox
            #1%
%           \hspace{\parindent}%
            \hspace{8pt}%
        }%
    }%
\endgroup
\ignorespaces
}

\begin{document}

\section{Dummy}

\parnote{Keyword 1} This paragraph has no indent.

\bigskip

\parnote{Other Word} This paragraph has an indent.

\end{document}

enter image description here

Werner
  • 603,163
Jörg
  • 7,653

2 Answers2

2

The tabto package is ideal for this sort of thing. Best of all, the \tabto can be issued at any time, even partway through the line. Here I demonstrate with the box even further left than an \llap, but it could be made similar by doing the \tabto to -2cm, rather than -1 in.

\documentclass{scrartcl}
\usepackage{tabto}
\newcommand\mnote[1]{\tabto*{-1in}\framebox[2cm][r]{#1 }\tabto{\TabPrevPos}}
\begin{document}
\section{Dummy}
this is a test\mnote{item}. continue\ldots

\bigskip

And now\mnote{item 2}, for another test.
\end{document}

enter image description here

1

Here is one option using zref's savepos module to position the box flush with the left margin. Some guidance it taken from I want to indent the next line by an exactly specified position:

enter image description here

\documentclass{scrartcl}
\usepackage{zref-savepos}
\makeatletter
% \zsaveposx is defined since 2011/12/05 v2.23 of zref-savepos
\@ifundefined{zsaveposx}{\let\zsaveposx\zsavepos}{}
\newcounter{hposcnt}
\renewcommand*{\thehposcnt}{hpos\number\value{hposcnt}}
\newcommand*{\parnote}[1]{% \parnote{<stuff>}
  \leavevmode%
  \stepcounter{hposcnt}%
  \zsaveposx{\thehposcnt}%
  \zref@refused{\thehposcnt}%
  \kern\dimexpr-\zposx{\thehposcnt}sp+1in+\oddsidemargin\relax%
  {\setlength{\fboxsep}{0pt}%
   \llap{\framebox[3cm][r]{\strut#1\hspace{8pt}}}}%
  \kern\dimexpr-1in-\oddsidemargin+\zposx{\thehposcnt}sp\relax%
  \ignorespaces
}
\makeatother

\begin{document}

\section{Dummy}


\parnote{Keyword 1} This paragraph has no indent.

\bigskip

\parnote{Other Word} This paragraph has an indent.

\bigskip

\noindent\parnote{Final word} This paragraph has an indent.

\end{document}

Since zref uses "labels" to identify the location, this requires at least two compiles until the references settle.

Werner
  • 603,163