二维码对齐

二维码对齐

请告诉我如何将二维码与其中一个角落对齐?

在此处输入图片描述

笔记: \qrcode[height=3cm]{https://github.com/AnMnv/eBook}可以正常工作,但彩色二维码不行

\documentclass[14pt]{extreport}
\usepackage[left=1.5cm,right=1.0cm,top=1cm,bottom=1.5cm,bindingoffset=0cm]{geometry}
\usepackage{qrcode}
\usepackage{tikz}

\usetikzlibrary{fadings, shadings}
\newcounter{fadcnt}\setcounter{fadcnt}{0}
\newcommand\fadingtext[3][]{%
\stepcounter{fadcnt}
\begin{tikzfadingfrompicture}[name=fading letter\thefadcnt]
\node[text=transparent!0,inner xsep=0pt,outer xsep=0pt,#1] {#3};
\end{tikzfadingfrompicture}%
\begin{tikzpicture}[baseline=(textnode.base)]
\node[inner sep=0pt,outer sep=0pt,#1](textnode){\phantom{#3}};
\shade[path fading=fading letter\thefadcnt,#2,fit fading=false]
(textnode.south west) rectangle (textnode.north east);%
\end{tikzpicture}}
\usetikzlibrary{calc}
\newbox\shbox
\tikzset{%
path picture shading/.style={%
path picture={%
\pgfpointdiff{\pgfpointanchor{path picture bounding box}{south west}}%
{\pgfpointanchor{path picture bounding box}{north east}}%
\pgfgetlastxy\pathwidth\pathheight%
\pgfinterruptpicture%
\global\setbox\shbox=\hbox{\pgfuseshading{#1}}%
\endpgfinterruptpicture%
\pgftransformshift{\pgfpointanchor{path picture bounding box}{center}}%
\pgftransformxscale{\pathwidth/(\wd\shbox)}%
\pgftransformyscale{\pathheight/(\ht\shbox)}% \dp will (should) be 0pt
\pgftext{\box\shbox}%
} } }
\pgfdeclarehorizontalshading{rainbow}{10bp}{color(0bp)=(violet);
color(1.6667bp)=(blue);
color(3.3333bp)=(cyan);
color(5bp)=(green);
color(6.6667bp)=(yellow);
color(8.3333bp)=(orange);
color(10bp)=(red)}


\begin{document}


\begin{tikzpicture}[remember picture,overlay]
\node[anchor=north west,yshift=0pt,xshift=0pt]%
at (current page.north west)
{\fadingtext[scale=0.5]{path picture shading=rainbow}{\qrcode[height=3cm]{https://github.com/AnMnv/eBook}}};
\end{tikzpicture}

\fadingtext[scale=0.5]{path picture shading=rainbow}{\qrcode[height=5cm]{https://github.com/AnMnv/eBook}}

\end{document}

答案1

问题很简单,节点的锚点是其中心,因此您的二维码会偏移该量。因此,您只需将其移回正确位置即可。由于您是从 TikZ 加载库,因此只需在代码部分calc进行坐标计算即可轻松完成此操作。at (...)

\documentclass[14pt]{extreport}
\usepackage[left=1.5cm,right=1.0cm,top=1cm,bottom=1.5cm,bindingoffset=0cm]{geometry}
\usepackage{qrcode}
\usepackage{tikz}

\usetikzlibrary{fadings, shadings}
\newcounter{fadcnt}\setcounter{fadcnt}{0}
\newcommand\fadingtext[3][]{%
\stepcounter{fadcnt}
\begin{tikzfadingfrompicture}[name=fading letter\thefadcnt]
\node[text=transparent!0,inner xsep=0pt,outer xsep=0pt,#1] {#3};
\end{tikzfadingfrompicture}%
\begin{tikzpicture}[baseline=(textnode.base)]
\node[inner sep=0pt,outer sep=0pt,#1](textnode){\phantom{#3}};
\shade[path fading=fading letter\thefadcnt,#2,fit fading=false]
(textnode.south west) rectangle (textnode.north east);%
\end{tikzpicture}}
\usetikzlibrary{calc}
\newbox\shbox
\tikzset{%
path picture shading/.style={%
path picture={%
\pgfpointdiff{\pgfpointanchor{path picture bounding box}{south west}}%
{\pgfpointanchor{path picture bounding box}{north east}}%
\pgfgetlastxy\pathwidth\pathheight%
\pgfinterruptpicture%
\global\setbox\shbox=\hbox{\pgfuseshading{#1}}%
\endpgfinterruptpicture%
\pgftransformshift{\pgfpointanchor{path picture bounding box}{center}}%
\pgftransformxscale{\pathwidth/(\wd\shbox)}%
\pgftransformyscale{\pathheight/(\ht\shbox)}% \dp will (should) be 0pt
\pgftext{\box\shbox}%
} } }
\pgfdeclarehorizontalshading{rainbow}{10bp}{color(0bp)=(violet);
color(1.6667bp)=(blue);
color(3.3333bp)=(cyan);
color(5bp)=(green);
color(6.6667bp)=(yellow);
color(8.3333bp)=(orange);
color(10bp)=(red)}


\begin{document}


\begin{tikzpicture}[remember picture,overlay]
\node at ($(current page.north west)+(.75cm,-.75cm)$) 
    {\fadingtext[scale=0.5]{path picture shading=rainbow}
    {\qrcode[height=3cm]{https://github.com/AnMnv/eBook}}};
\end{tikzpicture}


\end{document}

代码输出

相关内容