1

After solving the problem I had with the vertical alignment of the margin notes (https://tex.stackexchange.com/a/721419/13180), I am facing a similar problem. I can't control the vertical position of a box in the margin. What I want to do is to place a QR code with a text underneath. Depending on the insertion point, it should be positioned upwards (default option), downwards, or somewhere in between.

With the previous answer, I've worked out that it should be placed down, but I'm not able to get the default case. Here is a minimal example:

\documentclass[a4paper,10pt,twoside]{article}
\usepackage[spanish]{babel}
\usepackage{blindtext}
\usepackage[strict]{changepage}
\usepackage{marginnote}
\usepackage{calc}
\usepackage{qrcode}
\usepackage{tabularray}


%\renewcommand*{\marginfont}{\normalfont\footnotesize}
\setlength{\parindent}{0pt}

\NewDocumentCommand{\regla}{}{\hfill\rule{2cm}{\heightof{X}}}
 
\newcommand{\margen}[2][0pt]{%
  \marginnote{%
    \normalfont\footnotesize
    \checkoddpage    
    \parbox[t]{\marginparwidth}{%
      \ifoddpage\raggedright\else\raggedleft\fi#2%
    }%
  }[\dimeval{#1-\baselineskip}]
}


%% Comandos para generar los QR
\newlength\alturacaja

\newsavebox{\myqrbox}
\setlength{\fboxsep}{0pt}

\newcommand{\myqrcontent}[2]{%
  \qrcode[height=0.5in]{#1}\par%
  \textsf{\footnotesize #2}%
}

\newcommand{\myqrcontenti}[2]{%
  \begin{tblr}{%
      width=0.45\marginparwidth,%
      colspec={X[r,t]},%
      hspan = minimal,%
      vspan=even,%
      hlines,%
      vlines,%
      rowsep=0pt,%
      colsep=0pt,%
      cells = {font = \normalfont\footnotesize\sf},
    }
    \qrcode[height=0.5in]{#1}\\
     #2%
  \end{tblr}
}

\newcommand\myqrcommand[2]{%
  \checkoddpage
  \global\sbox\myqrbox{%
    %\fbox{%
       \begin{minipage}[b][][t]{0.75\marginparwidth}%
      %   \parbox[t]%
      % %   [][b]%
      %   {0.8\marginparwidth}{%
      %   \normalfont\footnotesize%
      %   \ifoddpage\raggedright\else\raggedleft\fi%
       \myqrcontent{#1}{#2}%
      % }
       \end{minipage}   
    %}
  }%
  
  % \global%
  \setlength{\alturacaja}{\dimeval{\ht\myqrbox+\dp\myqrbox}}
  \rule{1pt}{\alturacaja}
  \usebox{\myqrbox}
  \rule{1pt}{\alturacaja}
}

\begin{document}

\section[Dos]{Dos}
\blindtext[1]%

This line is the ``anchor point''.\regla%
\marginnote{%
  \myqrcommand{https://tex.stackexchange.com/}{This note should be aligned with the baseline of the anchor point.The correct fit depends (maybe?) on $n$\texttt{\textbackslash{}baselineskip}}
}%
%[-\dimeval{\ht\myqrbox+\baselineskip}] %%% These are all tests of settings that don't work %%%%%
[-\dimeval{3\baselineskip+0.5in}]%
%[-\dimeval{\ht\myqrbox+\dp\myqrbox}]%
%[-\dimeval{\ht\myqrbox}]%


\section[Tres]{Tres}

\marginnote{%
  \myqrcommand{https://tex.stackexchange.com/}{When the text drops down, the vertical adjustment is correct, as in any margin note.}%
}%
[-\baselineskip]%
\blindtext[2]

\end{document}

enter image description here

Sidebars are placed just to check that I'm measuring the height of the box correctly. I would also like to be able to set this length globally.

In the example, you can see another attempt to define \myqrcontent, using a table environment (tblr).

Maybe, marginnote is not the right package to do what I want.

All suggestions are wellcome.

Best regards.

1 Answer 1

1

You can maybe embed a tcolorbox in a \marginpar. It allows to test if the page is odd or even with if odd page or oneside and set the baseline with box align.

Define, for example, a command \marginbox

\NewDocumentCommand{\marginbox}{ O{} m }
  {%
    \marginpar{%
      \begin{tcolorbox}
        [%
          blankest,
          if odd page or oneside={flush left}{flush right},
          box align=bottom,
          #1
        ]
        #2
      \end{tcolorbox}%
    }%
  }

Thus

  • \marginbox{Hello} produce a box in the margin, positioned upward.
  • \marginbox[box align=top]{Hello} produce a box in the margin, positioned downward.
  • \marginbox[box align=center]{Hello} produce a box in the margin centered.

You can also set more precisely the position with

\marginbox[box align=top, before={\vspace{1cm}}]{Hello}

(but it doesn't work well without top box alignment).

\marginpar positions are sometimes buggy. I suggest to use the package marginfit to solve that. marginfit use the \pdfsavepos functionnality from pdfTeX. With LuaTeX, for example, add

\let\pdfsavepos\savepos
\let\pdflastxpos\lastxpos
\let\pdflastypos\lastypos

Finally, you can also add

\if@nobreak\ifvmode\nobreak\fi\fi

at the end of the command definition (inside \makeatletter...\makeatother) to prevent your headings to stay at the bottom of a page (for example, if a\marginbox immediately follows a \section).

Full example:

\documentclass[a4paper,10pt,twoside]{article}

\usepackage[spanish]{babel}
\usepackage{blindtext}
\usepackage{calc}
\usepackage{qrcode}

\usepackage[many]{tcolorbox}
\RequirePackage{marginfit}

%%% use marginfit with LuaTeX (uncomment if needed)
% \let\pdfsavepos\savepos
% \let\pdflastxpos\lastxpos
% \let\pdflastypos\lastypos
%%%%%%%%%%%%%%%%%%%%

\NewDocumentCommand{\regla}{}{\hfill\rule{2cm}{\heightof{X}}}

\newcommand{\myqrcontent}[2]{%
  \qrcode[height=0.5in]{#1}\par%
  \textsf{\footnotesize #2}%
}

\makeatletter
\NewDocumentCommand{\marginbox}{ O{} m }
  {%
    \marginpar{%
      \begin{tcolorbox}
        [%
          blankest,
          if odd page or oneside={flush left}{flush right},
          box align=bottom,
          #1
        ]
        #2
      \end{tcolorbox}%
      \if@nobreak\ifvmode\nobreak\fi\fi% prevent headings at bottom of the page
    }%
  }
\makeatother

\begin{document}

\section[Dos]{Dos}
\blindtext[1]%

This line is the ``anchor point''.\regla%
\marginbox{%
  \myqrcontent{https://tex.stackexchange.com/}{This note should be aligned with the baseline of the anchor point.The correct fit depends (maybe?) on $n$\texttt{\textbackslash{}baselineskip}}
}%

\section[Tres]{Tres}

\marginbox[box align=top]{%
  \myqrcontent{https://tex.stackexchange.com/}{When the text drops down, the vertical adjustment is correct, as in any margin note.}%
}%
This line is the ``anchor point''.\regla%

\blindtext[1]

This line is the ``anchor point''.\regla%
\marginbox[box align=top, before={\vspace*{1cm}}]{%
  \myqrcontent{https://tex.stackexchange.com/}{When the text drops down, the vertical adjustment is correct, as in any margin note.}%
}%

\blindtext[1]

\end{document}

Full example

You must log in to answer this question.

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