如何将 \includegraphics 图形与 \Worldflag 图形垂直对齐?

如何将 \includegraphics 图形与 \Worldflag 图形垂直对齐?

如何将一个\includegraphics图形与另一个\Worldflag图形垂直对齐?如您在以下示例中所见,fig1.png始终处于比图形更高的位置\Worldflag。我的目标是通过将图形调整 \includegraphics\Worldflag图形,使两个图形垂直对齐。

\documentclass{article}
\usepackage[a4paper,margin=0.1cm]{geometry} 
\usepackage{tabularray}
\usepackage{worldflags}
\usepackage{stackengine}
\flagsdefault[width=1\baselineskip]
\newcommand\Worldflag[1]{\addstackgap[.6\ht\strutbox]{%
  \belowbaseline[-0.4\ht\strutbox]{\worldflag{#1}}}}
\begin{document}

\begin{longtblr}{ colspec = {|X|X|},hlines}
First variable \newline Second variable & 
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\Worldflag{FR} \\
First variable & 
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\Worldflag{BR} \\
First variable \newline Second variable & 
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\Worldflag{CH} \\
\end{longtblr}

\end{document}

在此处输入图片描述


@John Kormylo 评论后第一次编辑:

如果我使用\worldflag而不是\Worldflag,我会得到以下结果,其中两个图形不再在表格单元格内垂直居中...相反,我想让两个图形在表格单元格内垂直居中...

\documentclass{article}
\usepackage[a4paper,margin=0.1cm]{geometry} 
\usepackage{tabularray}
\usepackage{worldflags}
\usepackage{stackengine}
\flagsdefault[width=1\baselineskip]
\newcommand\Worldflag[1]{\addstackgap[.6\ht\strutbox]{%
  \belowbaseline[-0.4\ht\strutbox]{\worldflag{#1}}}}
\begin{document}

\begin{longtblr}{ colspec = {|X|X|},hlines}
First variable \newline Second variable & 
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\worldflag{FR} \\
First variable & 
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\worldflag{BR} \\
First variable \newline Second variable & 
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\worldflag{CH} \\
\end{longtblr}

\end{document}

在此处输入图片描述


@John Kormylo 评论后第二次编辑:

\documentclass{article}
\usepackage[a4paper,margin=0.1cm]{geometry} 
\usepackage{tabularray}
\usepackage{worldflags}
\usepackage{stackengine}

\usepackage{tabularx} % <-- @John Kormylo comment
\flagsdefault[width=1\baselineskip]
\newcommand\Worldflag[1]{\addstackgap[.6\ht\strutbox]{%
  \belowbaseline[-0.4\ht\strutbox]{\worldflag{#1}}}}
\begin{document}
\renewcommand{\tabularxcolumn}[1]{>{\small}m{#1}} % <-- @John Kormylo comment


\begin{longtblr}{ colspec = {|X|X|},hlines}
First variable \newline Second variable & 
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\worldflag{FR} \\
First variable & 
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\worldflag{BR} \\
First variable \newline Second variable & 
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\worldflag{CH} \\
\end{longtblr}

\end{document}

在此处输入图片描述


第三次编辑...不知何故起作用了,但不是完美的垂直对齐(当左列的文本有多行时),并且周围有恼人的空白...此外,我想要一个比更简单的解决方案minipage,但我找不到它...

\documentclass{article}
\usepackage[a4paper,margin=0.1cm]{geometry} 
\usepackage{tabularray}
\usepackage{worldflags}
\usepackage{stackengine}
\flagsdefault[width=1\baselineskip]
\newcommand\Worldflag[1]{\addstackgap[.6\ht\strutbox]{%
  \belowbaseline[-0.4\ht\strutbox]{\worldflag{#1}}}}
\begin{document}



\begin{longtblr}{ colspec = {|X|X|},hlines}
First variable \newline Second variable & 
\begin{minipage}{0.5cm}
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\end{minipage}
\begin{minipage}{0.5cm}
\Worldflag{FR}
\end{minipage}
\\
First variable & 
\begin{minipage}{0.5cm}
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\end{minipage}
\begin{minipage}{0.5cm}
\Worldflag{BR}
\end{minipage}
\\
First variable \newline Second variable & 
\begin{minipage}{0.5cm}
\includegraphics[keepaspectratio,width=5mm]{fig1.png}
\end{minipage}
\begin{minipage}{0.5cm}
\Worldflag{CH}
\end{minipage}
\\
\end{longtblr}

\end{document}

在此处输入图片描述

答案1

注意\dp\strutbox=\baselineskip - \ht\strutbox

\documentclass{article}
\usepackage[a4paper,margin=0.1cm]{geometry} 
\usepackage{tabularray}
\usepackage{worldflags}
\usepackage{stackengine}

\flagsdefault[width=1\baselineskip]
\newcommand\Worldflag[1]{\addstackgap[.6\ht\strutbox]{%
  \belowbaseline[-0.4\ht\strutbox]{\worldflag{#1}}}}
\begin{document}


\begin{longtblr}{ colspec = {|X|X|},hlines}
First variable \newline Second variable & 
\raisebox{\dimexpr -\dp\strutbox-0.5\height}{% two lines
\includegraphics[keepaspectratio,width=5mm]{example-image}}
\raisebox{\dimexpr -\dp\strutbox-0.5\height}{% two lines
\worldflag{FR}} \\
First variable & 
\raisebox{\dimexpr \dp\strutbox-0.5\height}{% one line
\includegraphics[keepaspectratio,width=5mm]{example-image}}
\raisebox{\dimexpr \dp\strutbox-0.5\height}{% one line
\worldflag{BR}} \\
First variable \newline Second variable & 
\raisebox{\dimexpr -\dp\strutbox-0.5\height}{% two lines
\includegraphics[keepaspectratio,width=5mm]{example-image}}
\raisebox{\dimexpr -\dp\strutbox-0.5\height}{% two lines
\worldflag{CH}} \\
\end{longtblr}

\end{document}

相关内容