1

I want to put a QR code together with a counter and a text. I have defined a command, \qrblock, which to vertically align all elements uses a table (tblr). I want all the elements to be aligned at the top of the cell. When the text is long, an incorrect vertical space appears. Any suggestions?

Thanks in advance

Here is a mwe.

\documentclass[10pt,twoside,openrigth,numbers=noenddot]{book}

\usepackage{iftex}
\ifLuaTeX
% ⟨material for LuaTEX⟩
\let\pdfsavepos\savepos
\let\pdflastxpos\lastxpos
\let\pdflastypos\lastypos
\else
% ⟨material not for LuaTEX⟩
\fi

\usepackage{tabularray}
\usepackage{blindtext}
\usepackage[hyperlinks]{qrcode}

\newcounter{qrcounter}
\counterwithin{qrcounter}{section}

\newcommand{\qrblock}[2]{%
  \stepcounter{qrcounter}
  \setlength{\parindent}{0pt}
  \begin{tblr}{
      width = 0.5\linewidth,
      colspec = {Q[b,c]X[h,l]},
      hspan = minimal,
      hline{1-2} = {}, % only for testing
    }
    \qrcode[height=0.5in]{#1}%
    &  \textsf{\small QR \theqrcounter\\ #2 }%
  \end{tblr}
}

\begin{document}
\chapter{Chapter}
\section{Section}
\blindtext

\qrblock{https://tex.stackexchange.com}{Lorem ipsum (Correct)}%

\qrblock{https://tex.stackexchange.com}{Lorem ipsum dolor sit amet, consectetuer (Correct)}%

\qrblock{https://tex.stackexchange.com}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit  consectetuer adipiscing elit (Incorrect)}%

\section{Another section}
\blindtext
\end{document}

2 Answers 2

3

You could use the adjustbox package:

\documentclass[10pt,twoside,openrigth,numbers=noenddot]{book}

\usepackage{iftex}
\ifLuaTeX
% ⟨material for LuaTEX⟩
\let\pdfsavepos\savepos
\let\pdflastxpos\lastxpos
\let\pdflastypos\lastypos
\else
% ⟨material not for LuaTEX⟩
\fi

\usepackage{tabularray}
\usepackage{blindtext}
\usepackage[hyperlinks]{qrcode}

\newcounter{qrcounter}
\counterwithin{qrcounter}{section}
\usepackage{adjustbox}

\newcommand{\qrblock}[2]{%
  \stepcounter{qrcounter}
  \setlength{\parindent}{0pt}
  \begin{tblr}{
      width = 0.5\linewidth,
      colspec = {Q[t,c]X[t,l]},
      hspan = minimal,
      hline{1-2} = {}, % only for testing
    }
    \adjustbox{valign=t}{\qrcode[height=0.5in]{#1}}%
    &  \textsf{\small QR \theqrcounter\\ #2 }%
  \end{tblr}
}

\begin{document}
\chapter{Chapter}
\section{Section}
\blindtext

\qrblock{https://tex.stackexchange.com}{Lorem ipsum (Correct)}%

\qrblock{https://tex.stackexchange.com}{Lorem ipsum dolor sit amet, consectetuer (Correct)}%

\qrblock{https://tex.stackexchange.com}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit  consectetuer adipiscing elit (Incorrect)}%

\section{Another section}
\blindtext
\end{document}

enter image description here

1

Another way is use stackengine and h option for cell content align:

\documentclass[10pt,twoside,openrigth,numbers=noenddot]{book}
\usepackage{tabularray}
\usepackage{blindtext}
\newcounter{qrcounter}
\counterwithin{qrcounter}{section}
\usepackage{stackengine}
\usepackage[hyperlinks]{qrcode}

\newcommand{\qrblock}[2]%
{ 
  \stepcounter{qrcounter}
  \setlength{\parindent}{0pt}
\begin{tblr}{
      width = 0.5\linewidth,
      colspec = {Q[c]X[c, h, font=\small\linespread{0.84}\selectfont]},
      hspan   = minimal,
      hlines    % only for testing
    }
\stackon{}{\qrcode[height=0.5in]{#1}}%
    &  \textsf{QR \theqrcounter\\ #2 }%
  \end{tblr}
}

\begin{document}
\chapter{Chapter}
\section{Section}
\blindtext

\qrblock{https://tex.stackexchange.com}{Lorem ipsum (Correct)}

\qrblock{https://tex.stackexchange.com}{Lorem ipsum dolor sit amet, consectetuer (Correct)}

\qrblock{https://tex.stackexchange.com}{Lorem ipsum dolor sit amet, consectetuer adipiscing elit  consectetuer adipiscing elit (Incorrect)}%


\section{Another section}
\blindtext
\end{document}

Compiled by LuaLaTeX engine the above MWE gives (the same result is at use pdfLaTeX):

enter image description here

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.