\ Href içinde \ DTLfetch kullanma
LaTeX'te, çağrılan komutta bir parametre olarak belirtilen yerle ilişkili koordinatlara işaret eden bir Google Haritası URL'si oluşturan yeni bir komut oluşturmaya çalışıyorum (bir tür "tablo arama" işlevi). Yerler ve ilgili koordinatlar bir coords.CSVdosyanın içinde saklanır ve datatoolpaket kullanılarak okunmalıdır .
Google Harita URL'si şu şekilde yapılandırılmalıdır:
https://www.google.com/maps/?q=<LAT>,<LNG>
Bu şekilde yapılandırılan dosyadan yüklenen enlem ve boylam koordinatları nerede <LAT>ve nerede?<LNG>coords.CSV
Place,LAT,LNG
Test,42.0000,42.0000
...
Komut şu şekilde tanımlandı:
\usepackage{datatool}
\newcommand{\coords}[1]{
% Loads the CSV
\DTLsetseparator{,}
\DTLloaddb{coords}{doc/coords.csv}
% Assigns the coordinates to the variables \LAT and \LNG, relative to specific place (the parameter #1)
\def \LAT {\DTLfetch{coords}{Place}{#1}{LAT}}
\def \LNG {\DTLfetch{coords}{Place}{#1}{LNG}}
% Generates the URL pointing to Google Maps
Place: \href{https://www.google.com/maps/?q=\LNG ,\LNG}{#1}
}
Son olarak, yeni komutu şu şekilde kullanıyorum:
\coords{Test}
Komutun içinde çağrılan yerin koordinatlarını doğru bir şekilde yüklemeyi başardım (bu durumda "Test"), ancak URL'yi oluşturmaya çalıştığımda, LaTeX bana çoğu hata veren birçok hata veriyor ! Undefined control sequence. Ben kaldırırsanız \LATve \LNGURL (komut tanımı içinde) oluşturulur hattan, ben herhangi bir hata alamadım ama içinde saklanır beri ders URL, herhangi koordinatları içermez \LATve \LNGdeğişkenler.
\hrefKomutun içindeki tanımlanmış değişkenleri kullanarak URL'yi düzgün bir şekilde oluşturmanın bir yolu var mı ?
Bu bir test örneğidir:
\documentclass[a4paper,10pt]{article}
\usepackage{hyperref}
\usepackage{datatool}
\newcommand{\coords}[1]{
% Loads the CSV
\DTLsetseparator{,}
\DTLloaddb{coords}{coords.csv}
% Assigns the coordinates to the variables \LAT and \LNG, relative to specific place (the parameter #1)
\def \LAT {\DTLfetch{coords}{Place}{#1}{LAT}}
\def \LNG {\DTLfetch{coords}{Place}{#1}{LNG}}
% Generates the URL pointing to Google Maps
Place: \href{https://www.google.com/maps/?q=\LAT ,\LNG}{#1}
}
\begin{document}
\coords{Test}
\end{document}
Yanıtlar
"\ DTLfetch" alt dizesini almak için cevabımdaki aynı numarayı kullanabilirsiniz.
\begin{filecontents*}{\jobname.csv}
Place,LAT,LNG
Test,42.0000,42.0000
\end{filecontents*}
\documentclass{article}
\usepackage{datatool}
\usepackage{hyperref}
\DTLsetseparator{,}
\DTLloaddb{coords}{\jobname.csv}
\newcommand{\DTLfetchsave}[5]{% see https://tex.stackexchange.com/a/335489/4427
\edtlgetrowforvalue{#2}{\dtlcolumnindex{#2}{#3}}{#4}%
\dtlgetentryfromcurrentrow{\dtlcurrentvalue}{\dtlcolumnindex{#2}{#5}}%
\let#1\dtlcurrentvalue
}
\newcommand{\coords}[1]{%
\DTLfetchsave{\LAT}{coords}{Place}{#1}{LAT}%
\DTLfetchsave{\LNG}{coords}{Place}{#1}{LNG}%
% Generates the URL pointing to Google Maps
Place: \href{https://www.google.com/maps/?q=\LAT,\LNG}{#1}%
}
\begin{document}
\coords{Test}
\end{document}
Kullandığım \jobnamedosyalarımı clobbering önlemek için. Veritabanı için istediğiniz dosya adını kullanabilirsiniz.
Veritabanını her aradığınızda değil, bir kez yükleyin \coords.
Aşağıdaki yaklaşımı öneriyorum:
\documentclass[a4paper,10pt]{article}
% Let's create the file coords.csv - the directory ./doc must exist
% and writing-permission for that directory must be given!!!
% An already existing file won't be overwritten by the
% filecontents*-environment (unless you provide the "overwrite"-option)
% and you will be informed about the fact that the file already
% exists via a message in the .log-file only. You won't get a
% message on the terminal/console.
\begin{filecontents*}{doc/coords.csv}
Place,LAT,LNG
Test,42.0000,42.0000
\end{filecontents*}
\usepackage{hyperref}
\usepackage{datatool}
\newcommand{\coords}[1]{%%%
\begingroup
% Load the CSV only if database "coords" doesn't already exist:
\DTLifdbexists{coords}{}{%%%
%\DTLsetseparator{,}% Comma is the default, so this probably is not needed.
\DTLloaddb{coords}{doc/coords.csv}%%%
}%%%
% Assign the coordinates of the place whose name is denoted by the
% parameter #1 to the macros \LAT and \LNG:
\edtlgetrowforvalue{coords}{\dtlcolumnindex{coords}{Place}}{#1}%%%
\dtlgetentryfromcurrentrow{\LAT}{\dtlcolumnindex{coords}{LAT}}%%%
\dtlgetentryfromcurrentrow{\LNG}{\dtlcolumnindex{coords}{LNG}}%%%
%%%
% Use the name (denoted by #1) of the place as a hyperlink leading
% to the corresponding URL of Google Maps:
Place: \href{https://www.google.com/maps/?q=\LAT,\LNG}{#1}%%%
\endgroup
}%%%
\begin{document}
\coords{Test}
\end{document}
Bu yaklaşımı öneriyorum çünkü kodunuzla ilgili bazı sorunlar var:
Sorun 1:
Kişisel \coords-command istenmeyen uzay-belirteçleri ve üretir \par-tokens:
Bu istenmeyen belirteçlerin nerede ortaya çıktığını belirten yorumlarla yeniden yazıyorum:
\newcommand{\coords}[1]{ %<- unwanted space-token yields horizontal space in horizontal mode
% Loads the CSV
\DTLsetseparator{,} %<- unwanted space-token yields horizontal space in horizontal mode
\DTLloaddb{coords}{doc/coords.csv} %<- unwanted space-token yields horizontal space in horizontal mode
%<- unwanted control word token \par
% Assigns the coordinates to the variables \LAT and \LNG, relative to specific place (the parameter #1)
\def \LAT {\DTLfetch{coords}{Place}{#1}{LAT}} %<- unwanted space-token yields horizontal space in horizontal mode
\def \LNG {\DTLfetch{coords}{Place}{#1}{LNG}} %<- unwanted space-token yields horizontal space in horizontal mode
%<- unwanted control word token \par
% Generates the URL pointing to Google Maps
Place: \href{https://www.google.com/maps/?q=\LAT ,\LNG}{#1} %<- unwanted space-token yields horizontal space in horizontal mode
}
Sorun 2:
Hyperref kılavuzu, URL argümanındaki belirteçlerin \hreftamamen genişletilebilir olması gerektiğini söyler .
Komutlarınız \LATve \LNGtam olarak genişletilemez çünkü tanımları kontrol kelimesini \DTLfetchiçerirken, datatool paketinin kılavuzu açıkça şunu söylüyor
\DTLfetch{students}{regnum}{\RegNum}{forename} eşittir
Bu, bir makro tarafından tanımlandığında\dtlgetrowforvalue{students}{\dtlcolumnindex{students}{regnum}}{\RegNum}%
\dtlgetentryfromcurrentrow{\dtlcurrentvalue}{\dtlcolumnindex{students}{forename}}% \dtlcurrentvalue
\DTLfetch(ve dolayısıyla bir aşamada genişlemesi jeton veren her makro
\DTLfetch) tam olarak genişletilebilir olmadığını gösterir
.
\dtlcurrentvalue\dtlgetentryfromcurrentrow
Sorun 3:
Veritabanını her çağrı ile yüklemenin gerekli olduğundan şüpheliyim \coords.
Sorun 4:
\DTLsetseparatorVarsayılan değer olmasına rağmen, veritabanı girişleri için ayırıcıyı virgül olarak ayarlamak için komutu kullanırsınız . Yani bu muhtemelen modası geçmiş.