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}
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.