emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] ob-R.el, ox-latex.el: support for tikz graphics
@ 2013-02-24 18:51 Aaron Ecay
  2013-02-25 15:19 ` Nicolas Goaziou
  2013-02-26 18:25 ` [PATCH] ob-R.el, ox-latex.el: support for tikz graphics Achim Gratz
  0 siblings, 2 replies; 17+ messages in thread
From: Aaron Ecay @ 2013-02-24 18:51 UTC (permalink / raw)
  To: emacs-orgmode

Tikz graphics should be exported to LaTeX by \include, not as a link.
This commit changes the file extension used for tikz graphics from .tex
to .tikz.

TINYCHANGE
---
 lisp/ob-R.el     |  2 +-
 lisp/ox-latex.el | 22 +++++++++++++---------
 2 files changed, 14 insertions(+), 10 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 8db0853..9875f81 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -237,7 +237,7 @@ current code buffer."
 	 '((:bmp . "bmp")
 	   (:jpg . "jpeg")
 	   (:jpeg . "jpeg")
-	   (:tex . "tikz")
+	   (:tikz . "tikz")
 	   (:tiff . "tiff")
 	   (:png . "png")
 	   (:svg . "svg")
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 0ac251f..13da3f0 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -482,7 +482,7 @@ which format headlines like for Org version prior to 8.0."
   :type 'string)
 
 (defcustom org-latex-inline-image-rules
-  '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
+  '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\)\\'"))
   "Rules characterizing image files that can be inlined into LaTeX.
 
 A rule consists in an association whose key is the type of link
@@ -1737,6 +1737,7 @@ used as a communication channel."
 	 (path (let ((raw-path (org-element-property :path link)))
 		 (if (not (file-name-absolute-p raw-path)) raw-path
 		   (expand-file-name raw-path))))
+	 (filetype (file-name-extension path))
 	 (caption (org-latex--caption/label-string parent info))
 	 ;; Retrieve latex attributes from the element around.
 	 (attr (org-export-read-attribute :attr_latex parent))
@@ -1764,22 +1765,25 @@ used as a communication channel."
 			  ((org-string-nw-p opt) (format "[%s]" opt))
 			  ((eq float 'float) "[width=0.7\\textwidth]")
 			  ((eq float 'wrap) "[width=0.48\\textwidth]")
-			  (t "")))))
+			  (t ""))))
+	 (image-code (if (equal filetype "tikz")
+			 (format "\\input{%s}" path)
+		       (format "\\includegraphics%s{%s}" options path))))
     ;; Return proper string, depending on FLOAT.
     (case float
       (wrap (format "\\begin{wrapfigure}%s
 \\centering
-%s\\includegraphics%s{%s}
-%s\\end{wrapfigure}" placement comment-include options path caption))
+%s%s
+%s\\end{wrapfigure}" placement comment-include image-code caption))
       (multicolumn (format "\\begin{figure*}%s
 \\centering
-%s\\includegraphics%s{%s}
-%s\\end{figure*}" placement comment-include options path caption))
+%s%s
+%s\\end{figure*}" placement comment-include image-code caption))
       (figure (format "\\begin{figure}%s
 \\centering
-%s\\includegraphics%s{%s}
-%s\\end{figure}" placement comment-include options path caption))
-      (t (format "\\includegraphics%s{%s}" options path)))))
+%s%s
+%s\\end{figure}" placement comment-include image-code caption))
+      (t image-code))))
 
 (defun org-latex-link (link desc info)
   "Transcode a LINK object from Org to LaTeX.
-- 
1.8.1.4

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] ob-R.el, ox-latex.el: support for tikz graphics
  2013-02-24 18:51 [PATCH] ob-R.el, ox-latex.el: support for tikz graphics Aaron Ecay
@ 2013-02-25 15:19 ` Nicolas Goaziou
  2013-02-26 10:50   ` Achim Gratz
  2013-02-26 18:25 ` [PATCH] ob-R.el, ox-latex.el: support for tikz graphics Achim Gratz
  1 sibling, 1 reply; 17+ messages in thread
From: Nicolas Goaziou @ 2013-02-25 15:19 UTC (permalink / raw)
  To: Aaron Ecay; +Cc: emacs-orgmode

Hello,

Thanks for extracting that patch from the other one.

Aaron Ecay <aaronecay@gmail.com> writes:
> Tikz graphics should be exported to LaTeX by \include, not as a link.
> This commit changes the file extension used for tikz graphics from .tex
> to .tikz.

I'm wondering if it's really meant to be inserted as an inline image.
Does it support wrapfigure environment or width optional attribute?

If it doesn't, it may be better to implement a dedicated (or a more
general "input" function) formatting function that would be called from
`org-latex-link'.

What do you think?


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ob-R.el, ox-latex.el: support for tikz graphics
  2013-02-25 15:19 ` Nicolas Goaziou
@ 2013-02-26 10:50   ` Achim Gratz
  2013-02-26 12:25     ` Myles English
  2013-02-26 13:21     ` Nicolas Goaziou
  0 siblings, 2 replies; 17+ messages in thread
From: Achim Gratz @ 2013-02-26 10:50 UTC (permalink / raw)
  To: emacs-orgmode

Nicolas Goaziou <n.goaziou <at> gmail.com> writes:
> Aaron Ecay <aaronecay <at> gmail.com> writes:
> > Tikz graphics should be exported to LaTeX by \include, not as a link.
> > This commit changes the file extension used for tikz graphics from .tex
> > to .tikz.
> 
> I'm wondering if it's really meant to be inserted as an inline image.
> Does it support wrapfigure environment or width optional attribute?

A .tikz file usually contains a tikzpicture environment that wraps around the
actual tikz code.  It produces a box just like includegraphics does, only that
it is normal LaTeX code, and so you need \include to read it.

> If it doesn't, it may be better to implement a dedicated (or a more
> general "input" function) formatting function that would be called from
> `org-latex-link'.

I don't understand.  What I currently need to do for TikZ is this:

#+LaTeX: \begin{figure}[t]\centering
#+LaTeX:   \input{oldModel.tikz}
#+LaTeX:   \caption{Relative modelling error}
#+LaTeX: \label{fig:oldModel}
#+LaTeX: \end{figure}

With the patch that becomes much easier (especially if you want to bounce it to
HTML eventually, even though the HTML exporter would need to grok that the tikz
picture should first be converted to SVG or PNG or some other image format):

#+CAPTION:  Relative modelling error
#+LABEL: fig:oldModel
#+ATTR_LaTeX: :placement [t]
[[file:oldModel.tikz]]

Now, what I'd really like to do is to combine this with the production of the
tikz file by Gnuplot like so:

#+HEADERS:  :term "tikz size 15cm,5cm" :exports none :results yes
#+BEGIN_SRC gnuplot :file oldModel.tikz :cache yes...

#+RESULTS[4d90e02adddfd56f8fbf6bc99963b9bffe1f201e]:
#+CAPTION:  Relative modelling error for DP 0.7 model
#+LABEL: fig:oldModel
#+ATTR_LaTeX: :placement [t]
[[file:oldModel.tikz]]

That works, but I'll always have to remove some extra gunk between the new
result generated and the old one.


Regards,
Achim.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ob-R.el, ox-latex.el: support for tikz graphics
  2013-02-26 10:50   ` Achim Gratz
@ 2013-02-26 12:25     ` Myles English
  2013-02-26 13:21     ` Nicolas Goaziou
  1 sibling, 0 replies; 17+ messages in thread
From: Myles English @ 2013-02-26 12:25 UTC (permalink / raw)
  To: Achim Gratz; +Cc: emacs-orgmode


Hello,

'Scuse me for butting in but I would just like to share how I use tikz
graphics using the 'standalone' class, maybe its useful (and maybe not).
The advantage is that you can compile the tikz file by itself to see how
it looks and then \include it in the main document without having to
change it or compile it separately.

Achim Gratz writes:

> I don't understand.  What I currently need to do for TikZ is this:
>
> #+LaTeX: \begin{figure}[t]\centering
> #+LaTeX:   \input{oldModel.tikz}
> #+LaTeX:   \caption{Relative modelling error}
> #+LaTeX: \label{fig:oldModel}
> #+LaTeX: \end{figure}
>
> With the patch that becomes much easier (especially if you want to bounce it to
> HTML eventually, even though the HTML exporter would need to grok that the tikz
> picture should first be converted to SVG or PNG or some other image format):
>
> #+CAPTION:  Relative modelling error
> #+LABEL: fig:oldModel
> #+ATTR_LaTeX: :placement [t]
> [[file:oldModel.tikz]]
>
> Now, what I'd really like to do is to combine this with the production of the
> tikz file by Gnuplot like so:
>
> #+HEADERS:  :term "tikz size 15cm,5cm" :exports none :results yes
> #+BEGIN_SRC gnuplot :file oldModel.tikz :cache yes...
>
> #+RESULTS[4d90e02adddfd56f8fbf6bc99963b9bffe1f201e]:
> #+CAPTION:  Relative modelling error for DP0.7 model
> #+LABEL: fig:oldModel
> #+ATTR_LaTeX: :placement [t]
> [[file:oldModel.tikz]]

(This is not a MWE)

---------------------------------- tikz file ----------------------------------

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{calc,trees,positioning,arrows,chains,shapes.geometric,%
    decorations.pathreplacing,decorations.pathmorphing,shapes,%
    matrix,shapes.symbols,shadows}

\begin{document}
\input{/home/myles/docs/repo/graphics/tikz/tikz_style.tex}
\begin{tikzpicture}[rotate=-40]
\cylAxes;
%\draw[help grid] (0,0) grid (5,5);
\node[draw=none,inner sep=0] at (2,4) (yy) {};
\draw[inner sep=0] (0,0) -- +(90:4cm) arc (180:0:1cm and .5cm) node[name=nn] {};
\node[ellipse,
  rotate=-40,
  draw,anchor=west,
  minimum height=1cm,
  minimum width=2cm,
  inner sep=0] (0,0) (bb) {};
\draw (bb.east) -- (yy);
% domain
\draw[Domain boundary,
  color=red,
  inner sep=0] (bb.50) -- ++(40:8) -- ++(90:4) -- (bb.50|-+90:4.5) -- (bb.50);
\end{tikzpicture}
\end{document}

# ----------------------------- org doc -------------------------------------
#+LATEX_HEADER: \usepackage{standalone}
#+LATEX_CLASS: report
#+LATEX_CLASS_OPTIONS: [10pt,a4paper]
#+LATEX_HEADER: \DeclareGraphicsExtensions{.pdf,.png,.jpg,.mps}
#+LATEX_HEADER: \usepackage{subfig}
#+LATEX_HEADER: \usepackage{tikz}
#+LATEX_HEADER: \usetikzlibrary{shapes,arrows,fit,positioning,backgrounds}
#+LATEX_HEADER: \usetikzlibrary{decorations.pathreplacing} % for braces

#+begin_latex
\begin{figure}[htb]
\centering
\includestandalone[mode=tex,width=5cm]{/home/myles/docs/repo/graphics/tikz/2daxial}
\caption[2D linked domain]{\label{figure:2D_linked_domain}Tunnel domains.}
\end{figure}
#+end_latex

#-------------------------------------------------------------------

Myles

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ob-R.el, ox-latex.el: support for tikz graphics
  2013-02-26 10:50   ` Achim Gratz
  2013-02-26 12:25     ` Myles English
@ 2013-02-26 13:21     ` Nicolas Goaziou
  2013-02-26 14:33       ` Achim Gratz
  2013-02-26 19:21       ` Aaron Ecay
  1 sibling, 2 replies; 17+ messages in thread
From: Nicolas Goaziou @ 2013-02-26 13:21 UTC (permalink / raw)
  To: Achim Gratz; +Cc: emacs-orgmode

Hello,

Achim Gratz <Stromeko@NexGo.DE> writes:

> A .tikz file usually contains a tikzpicture environment that wraps around the
> actual tikz code.  It produces a box just like includegraphics does, only that
> it is normal LaTeX code, and so you need \include to read it.

Ok, thank you.

>> If it doesn't, it may be better to implement a dedicated (or a more
>> general "input" function) formatting function that would be called from
>> `org-latex-link'.
>
> I don't understand.  What I currently need to do for TikZ is this:
>
> #+LaTeX: \begin{figure}[t]\centering
> #+LaTeX:   \input{oldModel.tikz}
> #+LaTeX:   \caption{Relative modelling error}
> #+LaTeX: \label{fig:oldModel}
> #+LaTeX: \end{figure}

  #+begin_latex
  ...
  #+end_latex

would be easier.

> With the patch that becomes much easier 

[...]

I'm not questioning the usefulness of the patch. I want to know if
`org-latex--inline-image' is the appropriate function to provide that
feature or if the feature should be provided through another function.

In particular, `org-latex--inline-image' provide options to wrap the
picture in wrapfigure environment, or to set width optional argument for
includegraphics. If it doesn't make sense in this case, the code could
as well be moved into another function, like `org-latex--input-file',
which would handle options specific for \input.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ob-R.el, ox-latex.el: support for tikz graphics
  2013-02-26 13:21     ` Nicolas Goaziou
@ 2013-02-26 14:33       ` Achim Gratz
  2013-02-26 19:21       ` Aaron Ecay
  1 sibling, 0 replies; 17+ messages in thread
From: Achim Gratz @ 2013-02-26 14:33 UTC (permalink / raw)
  To: emacs-orgmode

Nicolas Goaziou <n.goaziou <at> gmail.com> writes:
> I'm not questioning the usefulness of the patch. I want to know if
> `org-latex--inline-image' is the appropriate function to provide that
> feature or if the feature should be provided through another function.
> 
> In particular, `org-latex--inline-image' provide options to wrap the
> picture in wrapfigure environment, or to set width optional argument for
> includegraphics. If it doesn't make sense in this case, the code could
> as well be moved into another function, like `org-latex--input-file',
> which would handle options specific for \input.

1) I don't know offhand what wrapfigure does, but as long as it just manipulates
boxes, I think it should work just fine with tikz.

2) Since \input doesn't take any options, you can't give it any.  Such options
would have to be given to the tikzpicture environment, which is inside the file
you are trying to include.  You can however nest tikzpicutre environments and
have options transfer from the outside to the inside, so if somebody gives you
options, just wrap the input statement in another tikzpicture environment.  But
the options are very different from includegraphics.  Scaling a tikzpicture you
should manipulate its box (resizebox or scalebox), but you'll almost almost want
to set the canvas on the original tikzpicture to the correct size so that the
text stays a sensible size.

Rgards,
Achim.

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ob-R.el, ox-latex.el: support for tikz graphics
  2013-02-24 18:51 [PATCH] ob-R.el, ox-latex.el: support for tikz graphics Aaron Ecay
  2013-02-25 15:19 ` Nicolas Goaziou
@ 2013-02-26 18:25 ` Achim Gratz
  2013-02-26 19:49   ` Aaron Ecay
  1 sibling, 1 reply; 17+ messages in thread
From: Achim Gratz @ 2013-02-26 18:25 UTC (permalink / raw)
  To: emacs-orgmode

Aaron Ecay writes:
> Tikz graphics should be exported to LaTeX by \include, not as a link.
> This commit changes the file extension used for tikz graphics from .tex
> to .tikz.

Thank you, this works nicely.  Instead of dropping the options you might
want to consider wrapping another tikzpicture environment around the
\input statement and applying any options there.


Regards,
Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

Factory and User Sound Singles for Waldorf Blofeld:
http://Synth.Stromeko.net/Downloads.html#WaldorfSounds

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ob-R.el, ox-latex.el: support for tikz graphics
  2013-02-26 13:21     ` Nicolas Goaziou
  2013-02-26 14:33       ` Achim Gratz
@ 2013-02-26 19:21       ` Aaron Ecay
  2013-02-26 19:22         ` [PATCH] ox-latex: provide width and height options for images Aaron Ecay
  1 sibling, 1 reply; 17+ messages in thread
From: Aaron Ecay @ 2013-02-26 19:21 UTC (permalink / raw)
  To: Nicolas Goaziou, Achim Gratz; +Cc: emacs-orgmode

2013ko otsailak 26an, Nicolas Goaziou-ek idatzi zuen:
> 
> I'm not questioning the usefulness of the patch. I want to know if
> `org-latex--inline-image' is the appropriate function to provide that
> feature or if the feature should be provided through another function.
> 
> In particular, `org-latex--inline-image' provide options to wrap the
> picture in wrapfigure environment, or to set width optional argument
> for includegraphics. If it doesn't make sense in this case, the code
> could as well be moved into another function, like
> `org-latex--input-file', which would handle options specific for
> \input.

As Achim pointed out, it is useful to wrap the \input{...} in figure,
wrapfig, etc. environments.  For resizing, one needs to use a \resizebox
command or similar.  (Also as Achim pointed out.)

In the message with id 877gm98irg.fsf@gmail.com, you pointed out the
possibility to factor out :width (and presumably :height) from :options.
This is the logical thing to do in order to support resizing of tikz
images.  (Putting aside the issue of text sizes becoming too big or too
small – but this is also a problem for the resizing of non-tikz images,
of course.)

Thus, I’ve implemented this (patch to follow, on top of the tikz support
patch).

I decided to use \resizebox for all width and height-setting, instead of
trying to condition on whether we’re using \includegraphics or \input.
This means that the code in its purest form is backwards-incompatible
with old documents that might be using :options width=... to set the
width of images, since the width would be set twice (once explicitly by
the options, once by default in the \resizebox).

I’ve thus left the default value of the org-latex-image-default-option
variable alone.  It should eventually be changed to move the width
setting to org-latex-image-default-width, but I’m not sure how long we
want the grace period to be.  (Maybe it should be short, since the new
exporter already introduced some incompatibilities).

People who want to do things the new way should set ...-default-option
to “” (or some value not making reference to width) and
...-default-width to “0.9\linewidth” in their initialization file.

(It’s possible to try to code around the problem by detecting if
someone’s options have width= in them, etc. – but I couldn’t figure out
how to do so without having an ugly mess of code.)

WDYT?

-- 
Aaron Ecay

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH] ox-latex: provide width and height options for images
  2013-02-26 19:21       ` Aaron Ecay
@ 2013-02-26 19:22         ` Aaron Ecay
  2013-02-26 23:04           ` Rasmus
  2013-02-27  8:23           ` Nicolas Goaziou
  0 siblings, 2 replies; 17+ messages in thread
From: Aaron Ecay @ 2013-02-26 19:22 UTC (permalink / raw)
  To: emacs-orgmode

These are implemented with \resizebox, and thus are uniform across
different types of image inclusion (\includegraphics, \input of tikz
images).  This differs from the older way of using width and height
optional args to \includegraphics.

Thus, the default value for org-latex-image-default-options is left
untouched, to avoid breaking compatibility with older code.  After a
transition period, the 0.9\linewidth value should be moved into
org-latex-image-default-width, and the -options variable set to the
empty string.

Also introduce a convenience function to org-macs.el.
---
 lisp/org-macs.el |  7 +++++++
 lisp/ox-latex.el | 29 ++++++++++++++++++++++++++---
 2 files changed, 33 insertions(+), 3 deletions(-)

diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index 172d5d1..3fe62cd 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -87,6 +87,13 @@
 Otherwise return nil."
   (and v (not (equal v "nil")) v))
 
+(defun org-not-nil-or-empty (v)
+  "Return V if V is not nil, the string \"nil\", or a string
+consisting of solely whitespace.  Otherwise return nil."
+  (and (org-not-nil v)
+       (org-string-nw-p v)
+       v))
+
 (defun org-substitute-posix-classes (re)
   "Substitute posix classes in regular expression RE."
   (let ((ss re))
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 13da3f0..d1f47b8 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -471,11 +471,24 @@ which format headlines like for Org version prior to 8.0."
 
 ;;;; Links
 
+;;; NOTE: this variable is for backwards compatibility; eventually the
+;;; width setting should be moved to the default-width variable, and
+;;; this set to the empty string.
 (defcustom org-latex-image-default-option "width=.9\\linewidth"
   "Default option for images."
   :group 'org-export-latex
   :type 'string)
 
+(defcustom org-latex-image-default-width ""
+  "Default option for images."
+  :group 'org-export-latex
+  :type 'string)
+
+(defcustom org-latex-image-default-height ""
+  "Default option for images."
+  :group 'org-export-latex
+  :type 'string)
+
 (defcustom org-latex-default-figure-position "htb"
   "Default position for latex figures."
   :group 'org-export-latex
@@ -1755,6 +1768,15 @@ used as a communication channel."
 		   (format "[%s]" org-latex-default-figure-position))
 		  (t ""))))
 	 (comment-include (if (plist-get attr :comment-include) "%" ""))
+	 ;; It is possible to specify width and height in the
+	 ;; ATTR_LATEX line, and also via default variables.
+	 (width (format "%s" (or (plist-get attr :width)
+				 org-latex-image-default-width)))
+	 (height (format "%s" (or (plist-get attr :height)
+				  org-latex-image-default-height)))
+	 (resize (format "\\resizebox{%s}{%s}{%%s}"
+			 (if (org-not-nil-or-empty width) width "!")
+			 (if (org-not-nil-or-empty height) height "!")))
 	 ;; Options for "includegraphics" macro. Make sure it is
 	 ;; a string with square brackets when non empty.  Default to
 	 ;; `org-latex-image-default-option' when possible.
@@ -1766,9 +1788,10 @@ used as a communication channel."
 			  ((eq float 'float) "[width=0.7\\textwidth]")
 			  ((eq float 'wrap) "[width=0.48\\textwidth]")
 			  (t ""))))
-	 (image-code (if (equal filetype "tikz")
-			 (format "\\input{%s}" path)
-		       (format "\\includegraphics%s{%s}" options path))))
+	 (image-code (format resize
+			     (if (equal filetype "tikz")
+				 (format "\\input{%s}" path)
+			       (format "\\includegraphics%s{%s}" options path)))))
     ;; Return proper string, depending on FLOAT.
     (case float
       (wrap (format "\\begin{wrapfigure}%s
-- 
1.8.1.4

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] ob-R.el, ox-latex.el: support for tikz graphics
  2013-02-26 18:25 ` [PATCH] ob-R.el, ox-latex.el: support for tikz graphics Achim Gratz
@ 2013-02-26 19:49   ` Aaron Ecay
  0 siblings, 0 replies; 17+ messages in thread
From: Aaron Ecay @ 2013-02-26 19:49 UTC (permalink / raw)
  To: Achim Gratz, emacs-orgmode

2013ko otsailak 26an, Achim Gratz-ek idatzi zuen:

> Thank you, this works nicely.  Instead of dropping the options you
> might want to consider wrapping another tikzpicture environment around
> the \input statement and applying any options there.
> 
>

This is a good idea.  To avoid a proliferation of patches, I will wait
to see what Nicolas and others think of the patch to separate handling
of :width and :height from :options.  Then I will send a new patch with
the original change, the :width/:height thing, and this idea about
:options all squashed together.

-- 
Aaron Ecay

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ox-latex: provide width and height options for images
  2013-02-26 19:22         ` [PATCH] ox-latex: provide width and height options for images Aaron Ecay
@ 2013-02-26 23:04           ` Rasmus
  2013-02-27  2:02             ` Aaron Ecay
  2013-02-27 18:40             ` Achim Gratz
  2013-02-27  8:23           ` Nicolas Goaziou
  1 sibling, 2 replies; 17+ messages in thread
From: Rasmus @ 2013-02-26 23:04 UTC (permalink / raw)
  To: emacs-orgmode

Aaron Ecay <aaronecay@gmail.com> writes:
> WDYT?
> [...]
> These are implemented with \resizebox, and thus are uniform across
> different types of image inclusion (\includegraphics, \input of tikz
> images).  This differs from the older way of using width and height
> optional args to \includegraphics.

I don't like it.  Why would we use resizebox rather than the the
arguments in includegraphics for pictures?  From a TeXnish perspective
it seems wrong and inelegant to me (totally subjective).

For tikz figures resizing should be done via the scale argument to
preserve the right font sizes.  If people want to resize a tikz figure
they should do so in their tikz code. 

That being said, better tikz handling in Org is a wonderful idea.

–Rasmus

-- 
Need more coffee. . .

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ox-latex: provide width and height options for images
  2013-02-26 23:04           ` Rasmus
@ 2013-02-27  2:02             ` Aaron Ecay
  2013-02-27 18:40             ` Achim Gratz
  1 sibling, 0 replies; 17+ messages in thread
From: Aaron Ecay @ 2013-02-27  2:02 UTC (permalink / raw)
  To: Rasmus, emacs-orgmode

2013ko otsailak 26an, Rasmus-ek idatzi zuen:
> I don't like it.  Why would we use resizebox rather than the the
> arguments in includegraphics for pictures?

Because it achieves the same result, but allows the operation to be more
uniform from the org side of things.  A \resizebox is something that org
can stuff any image into and have it be resized, so org doesn’t have to
deal with the different ways of resizing things.

One thing that might be suspected is that passing the width and height
to \includegraphics would allow it to scale down an image that is
included at less than 100% size, shrinking the resultant pdf.  But this
is not what happens (at least for jpg images), in my testing.

> For tikz figures resizing should be done via the scale argument to
> preserve the right font sizes.  If people want to resize a tikz figure
> they should do so in their tikz code.

There’s two ways of resizing tikz images: rescaling the distances
between things and leaving the text alone, or resizing the whole image
including the text.  This supports the latter.  Achim’s suggestion about
nested tikzpicture environments (which I intend to implement) takes care
of the former.

-- 
Aaron Ecay

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ox-latex: provide width and height options for images
  2013-02-26 19:22         ` [PATCH] ox-latex: provide width and height options for images Aaron Ecay
  2013-02-26 23:04           ` Rasmus
@ 2013-02-27  8:23           ` Nicolas Goaziou
  2013-03-06  4:02             ` aaronecay
  1 sibling, 1 reply; 17+ messages in thread
From: Nicolas Goaziou @ 2013-02-27  8:23 UTC (permalink / raw)
  To: Aaron Ecay; +Cc: emacs-orgmode

Hello,

Aaron Ecay <aaronecay@gmail.com> writes:

Thank you for your patch. Here are a few comments.

> These are implemented with \resizebox, and thus are uniform across
> different types of image inclusion (\includegraphics, \input of tikz
> images).  This differs from the older way of using width and height
> optional args to \includegraphics.

I tend to agree with Rasmus. It would be better to keep height and width
options in \includegraphics when possible.

> Thus, the default value for org-latex-image-default-options is left
> untouched, to avoid breaking compatibility with older code.  After a
> transition period, the 0.9\linewidth value should be moved into
> org-latex-image-default-width, and the -options variable set to the
> empty string.

We don't need this precaution. The exporter code for 8.0 introduced many
incompatibilities already. Also, this one is easy to discover.

> +(defun org-not-nil-or-empty (v)
> +  "Return V if V is not nil, the string \"nil\", or a string
> +consisting of solely whitespace.  Otherwise return nil."
> +  (and (org-not-nil v)
> +       (org-string-nw-p v)
> +       v))

I'm not sure it's worth creating a new function for it. Anyway, the
first line of a docstring should be a sentence on its own.

>  (defcustom org-latex-image-default-option "width=.9\\linewidth"
>    "Default option for images."
>    :group 'org-export-latex
>    :type 'string)

We can set it to "".

> +(defcustom org-latex-image-default-width ""
> +  "Default option for images."
> +  :group 'org-export-latex
> +  :type 'string)
> +
> +(defcustom org-latex-image-default-height ""
> +  "Default option for images."
> +  :group 'org-export-latex
> +  :type 'string)

I think it's a good step forward. It will need to be documented in the
comments at the beginning of ox-latex.el, where all attributes
properties relative to different syntactical elements are explained.

>  (defcustom org-latex-default-figure-position "htb"
>    "Default position for latex figures."
>    :group 'org-export-latex
> @@ -1755,6 +1768,15 @@ used as a communication channel."
>  		   (format "[%s]" org-latex-default-figure-position))
>  		  (t ""))))
>  	 (comment-include (if (plist-get attr :comment-include) "%" ""))
> +	 ;; It is possible to specify width and height in the
> +	 ;; ATTR_LATEX line, and also via default variables.
> +	 (width (format "%s" (or (plist-get attr :width)
> +				 org-latex-image-default-width)))
> +	 (height (format "%s" (or (plist-get attr :height)
> +				  org-latex-image-default-height)))
> +	 (resize (format "\\resizebox{%s}{%s}{%%s}"
> +			 (if (org-not-nil-or-empty width) width "!")
> +			 (if (org-not-nil-or-empty height) height "!")))

Here, you can obtain \resizebox{!}{!}{%s}, which is wrong, isn't it?

>  	 ;; Options for "includegraphics" macro. Make sure it is
>  	 ;; a string with square brackets when non empty.  Default to
>  	 ;; `org-latex-image-default-option' when possible.
> @@ -1766,9 +1788,10 @@ used as a communication channel."
>  			  ((eq float 'float) "[width=0.7\\textwidth]")
>  			  ((eq float 'wrap) "[width=0.48\\textwidth]")
>  			  (t ""))))

This needs to be changed as these options would interfere with :width
argument. For example, (eq float 'float) could set :width property if it
is undefined. Obviously, this means the check has to be done before
WIDTH and HEIGHT strings are built.

> -	 (image-code (if (equal filetype "tikz")
> -			 (format "\\input{%s}" path)
> -		       (format "\\includegraphics%s{%s}" options path))))
> +	 (image-code (format resize
> +			     (if (equal filetype "tikz")
> +				 (format "\\input{%s}" path)
> +			       (format "\\includegraphics%s{%s}" options path)))))

See comments above.

Thank you again,


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ox-latex: provide width and height options for images
  2013-02-26 23:04           ` Rasmus
  2013-02-27  2:02             ` Aaron Ecay
@ 2013-02-27 18:40             ` Achim Gratz
  1 sibling, 0 replies; 17+ messages in thread
From: Achim Gratz @ 2013-02-27 18:40 UTC (permalink / raw)
  To: emacs-orgmode

Rasmus writes:
> For tikz figures resizing should be done via the scale argument to
> preserve the right font sizes.  If people want to resize a tikz figure
> they should do so in their tikz code. 

No, that doesn't work for many tikz pictures; in particular it doesn't
work for tikz files produced by gnuplot.  There are reams of threads on
stackoverflow on this topic…


Regards,
Achim.
-- 
+<[Q+ Matrix-12 WAVE#46+305 Neuron microQkb Andromeda XTk Blofeld]>+

Samples for the Waldorf Blofeld:
http://Synth.Stromeko.net/Downloads.html#BlofeldSamplesExtra

^ permalink raw reply	[flat|nested] 17+ messages in thread

* Re: [PATCH] ox-latex: provide width and height options for images
  2013-02-27  8:23           ` Nicolas Goaziou
@ 2013-03-06  4:02             ` aaronecay
  2013-03-06  4:04               ` [PATCH] ox-latex: Support tikz images, :width, and :height Aaron Ecay
  2013-03-06  8:35               ` [PATCH] ox-latex: provide width and height options for images Nicolas Goaziou
  0 siblings, 2 replies; 17+ messages in thread
From: aaronecay @ 2013-03-06  4:02 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

2013ko otsailak 27an, Nicolas Goaziou-ek idatzi zuen:

> Thank you for your patch. Here are a few comments.

Thank you for the comments! I think the patch is simpler and better
thanks to them.

> 
>> These are implemented with \resizebox, and thus are uniform across
>> different types of image inclusion (\includegraphics, \input of tikz
>> images).  This differs from the older way of using width and height
>> optional args to \includegraphics.
> 
> I tend to agree with Rasmus. It would be better to keep height and
> width options in \includegraphics when possible.

Okay.

> 
>> Thus, the default value for org-latex-image-default-options is left
>> untouched, to avoid breaking compatibility with older code.  After a
>> transition period, the 0.9\linewidth value should be moved into
>> org-latex-image-default-width, and the -options variable set to the
>> empty string.
> 
> We don't need this precaution. The exporter code for 8.0 introduced
> many incompatibilities already. Also, this one is easy to discover.

Okay.

> 
> I think it's a good step forward. It will need to be documented in the
> comments at the beginning of ox-latex.el, where all attributes
> properties relative to different syntactical elements are explained.

Okay – see what you think of the documentation in the new patch.

> 
> Here, you can obtain \resizebox{!}{!}{%s}, which is wrong, isn't it?

No...it is a no-op.  That said, the code should no longer do this.

> 
> This needs to be changed as these options would interfere with :width
> argument. For example, (eq float 'float) could set :width property if
> it is undefined. Obviously, this means the check has to be done before
> WIDTH and HEIGHT strings are built.

This should now be handled.  The new patch will follow this email.  In
related news, I have completed the FSF copyright assignment process, so
the patch can be merged whenever it is deemed ready.

Thanks again,

-- 
Aaron Ecay

^ permalink raw reply	[flat|nested] 17+ messages in thread

* [PATCH] ox-latex: Support tikz images, :width, and :height
  2013-03-06  4:02             ` aaronecay
@ 2013-03-06  4:04               ` Aaron Ecay
  2013-03-06  8:35               ` [PATCH] ox-latex: provide width and height options for images Nicolas Goaziou
  1 sibling, 0 replies; 17+ messages in thread
From: Aaron Ecay @ 2013-03-06  4:04 UTC (permalink / raw)
  To: emacs-orgmode

* ox-latex.el:
(org-latex-image-default-option): Change default value to ""
(org-latex-image-default-width)
(org-latex-image-default-height): Add variables
(org-latex-inline-image-rules): Make .tikz files as exportable with
latex
(org-latex--inline-image): Support tikz images.  Also support separate
:width and :height parameters for images.

* ob-R.el (org-babel-R-construct-graphics-device-call): Change file
extension of tikz graphics files to .tikz

Tikz graphics should be exported to LaTeX by \include, not as a link.
This commit changes the file extension used for tikz graphics from .tex
to .tikz, and inserts code for including such images.  The :options for
tikz graphics are passed as an optional argument to a tikzpicture
environment.

Also provide :width and :height ATTR_LATEX entries for images.  For tikz
graphics, these are implemented with \resizebox; for other image types
they are inserted in the optional arguments to \includegraphics.
---
 lisp/ob-R.el     |   2 +-
 lisp/ox-latex.el | 111 ++++++++++++++++++++++++++++++++++++++++---------------
 2 files changed, 83 insertions(+), 30 deletions(-)

diff --git a/lisp/ob-R.el b/lisp/ob-R.el
index 8db0853..9875f81 100644
--- a/lisp/ob-R.el
+++ b/lisp/ob-R.el
@@ -237,7 +237,7 @@ current code buffer."
 	 '((:bmp . "bmp")
 	   (:jpg . "jpeg")
 	   (:jpeg . "jpeg")
-	   (:tex . "tikz")
+	   (:tikz . "tikz")
 	   (:tiff . "tiff")
 	   (:png . "png")
 	   (:svg . "svg")
diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 8a5b6a6..47b8bb1 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -77,15 +77,19 @@
 ;; (i.e. "inparaenum").  The second one allows to specify optional
 ;; arguments for that environment (square brackets are not mandatory).
 ;;
-;; Images accept `:float', `:placement', `:comment-include', and
-;; `:options' as attributes.  `:float' accepts a symbol among `wrap',
-;; `multicolumn', and `figure', which defines the float environment
-;; for the table (if unspecified, an image with a caption will be set
-;; in a "figure" environment).  `:comment-include' is a boolean that
-;; toggles whether to comment out the \includegraphics
-;; call.  `:placement' is a string that will be used as argument for
-;; the environment chosen.  `:options' is a string that will be used
-;; as the optional argument for "includegraphics" macro.
+;; Images accept `:float', `:placement', `:comment-include', `:width',
+;; and `:height', and `:options' as attributes.  `:float' accepts a
+;; symbol among `wrap', `multicolumn', and `figure', which defines the
+;; float environment for the image (if unspecified, an image with a
+;; caption will be set in a "figure" environment).  `:comment-include'
+;; is a boolean that toggles whether to comment out the code which
+;; actually includes the image. `:placement' is a string that will be
+;; used as argument for the environment chosen.  `:width' and
+;; `:height' control the width and height of the image.  `:options' is
+;; a string that will be used as the optional argument for
+;; "includegraphics" macro or (in the case of tikz images), used as
+;; the optional argument for a `tikzpicture' environment which will
+;; surround the "\input" picture code.
 ;;
 ;; Special blocks accept `:options' as attribute.  Its value will be
 ;; appended as-is to the opening string of the environment created.
@@ -472,18 +476,28 @@ which format headlines like for Org version prior to 8.0."
 
 ;;;; Links
 
-(defcustom org-latex-image-default-option "width=.9\\linewidth"
+(defcustom org-latex-image-default-option ""
   "Default option for images."
   :group 'org-export-latex
   :type 'string)
 
+(defcustom org-latex-image-default-width ".9\\linewidth"
+  "Default width for images."
+  :group 'org-export-latex
+  :type 'string)
+
+(defcustom org-latex-image-default-height ""
+  "Default height for images."
+  :group 'org-export-latex
+  :type 'string)
+
 (defcustom org-latex-default-figure-position "htb"
   "Default position for latex figures."
   :group 'org-export-latex
   :type 'string)
 
 (defcustom org-latex-inline-image-rules
-  '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\)\\'"))
+  '(("file" . "\\.\\(pdf\\|jpeg\\|jpg\\|png\\|ps\\|eps\\|tikz\\)\\'"))
   "Rules characterizing image files that can be inlined into LaTeX.
 
 A rule consists in an association whose key is the type of link
@@ -1751,6 +1765,7 @@ used as a communication channel."
 	 (path (let ((raw-path (org-element-property :path link)))
 		 (if (not (file-name-absolute-p raw-path)) raw-path
 		   (expand-file-name raw-path))))
+	 (filetype (file-name-extension path))
 	 (caption (org-latex--caption/label-string parent info))
 	 ;; Retrieve latex attributes from the element around.
 	 (attr (org-export-read-attribute :attr_latex parent))
@@ -1768,32 +1783,70 @@ used as a communication channel."
 		   (format "[%s]" org-latex-default-figure-position))
 		  (t ""))))
 	 (comment-include (if (plist-get attr :comment-include) "%" ""))
-	 ;; Options for "includegraphics" macro. Make sure it is
-	 ;; a string with square brackets when non empty.  Default to
-	 ;; `org-latex-image-default-option' when possible.
-	 (options (let ((opt (format "%s"
-				     (or (plist-get attr :options)
-					 org-latex-image-default-option))))
-		    (cond ((string-match "\\`\\[.*\\]" opt) opt)
-			  ((org-string-nw-p opt) (format "[%s]" opt))
-			  ((eq float 'float) "[width=0.7\\textwidth]")
-			  ((eq float 'wrap) "[width=0.48\\textwidth]")
-			  (t "")))))
+	 ;; It is possible to specify width and height in the
+	 ;; ATTR_LATEX line, and also via default variables.
+	 (width (format "%s" (or (plist-get attr :width)
+				 (cond
+				  ((eq float 'float) "0.7\\textwidth")
+				  ((eq float 'wrap) "0.48\\textwidth")
+				  (t org-latex-image-default-width)))))
+	 (height (format "%s" (or (plist-get attr :height)
+				  org-latex-image-default-height)))
+	 (options (or (plist-get attr :options)
+		      org-latex-image-default-option))
+	 image-code)
+    (if (not options)
+	(setq options "")
+      (setq options (format "%s" options)))
+    (when (string-match "\\`\\[\\(.*\\)\\]\\'" options)
+      (setq options (match-string 1 options)))
+    (if (equal filetype "tikz")
+	;; For tikz images:
+	;; - use \input to read in image file
+	;; - if options are present, wrap in a tikzpicture environment
+	;; - if width or height are present, use \resizebox to change
+	;;   the image size
+	(progn
+	  (setq image-code (format "\\input{%s}" path))
+	  (when (and (org-string-nw-p options))
+	    (setq image-code (format "\\begin{tikzpicture}[%s]\n%s\n\\end{tikzpicture}"
+				     options
+				     image-code)))
+	  (when (or (and (org-string-nw-p width) (org-not-nil width))
+		    (and (org-string-nw-p height) (org-not-nil height)))
+	    (setq image-code (format "\\resizebox{%s}{%s}{%s}"
+				     (if (and (org-string-nw-p width) (org-not-nil width))
+					 width
+				       "!")
+				     (if (and (org-string-nw-p height) (org-not-nil height))
+					 height
+				       "!")
+				     image-code))))
+      ;; For other images:
+      ;; - add width and height to options
+      ;; - include the image with \includegraphics
+      (when (and (org-string-nw-p width) (org-not-nil width))
+	(setq options (concat options ",width=" width)))
+      (when (and (org-string-nw-p height) (org-not-nil height))
+	(setq options (concat options ",height=" height)))
+      (when (= (aref options 0) ?,)
+	(setq options (substring options 1)))
+      (setq image-code (format "\\includegraphics[%s]{%s}" options path)))
     ;; Return proper string, depending on FLOAT.
     (case float
       (wrap (format "\\begin{wrapfigure}%s
 \\centering
-%s\\includegraphics%s{%s}
-%s\\end{wrapfigure}" placement comment-include options path caption))
+%s%s
+%s\\end{wrapfigure}" placement comment-include image-code caption))
       (multicolumn (format "\\begin{figure*}%s
 \\centering
-%s\\includegraphics%s{%s}
-%s\\end{figure*}" placement comment-include options path caption))
+%s%s
+%s\\end{figure*}" placement comment-include image-code caption))
       (figure (format "\\begin{figure}%s
 \\centering
-%s\\includegraphics%s{%s}
-%s\\end{figure}" placement comment-include options path caption))
-      (t (format "\\includegraphics%s{%s}" options path)))))
+%s%s
+%s\\end{figure}" placement comment-include image-code caption))
+      (t image-code))))
 
 (defun org-latex-link (link desc info)
   "Transcode a LINK object from Org to LaTeX.
-- 
1.8.1.5

^ permalink raw reply related	[flat|nested] 17+ messages in thread

* Re: [PATCH] ox-latex: provide width and height options for images
  2013-03-06  4:02             ` aaronecay
  2013-03-06  4:04               ` [PATCH] ox-latex: Support tikz images, :width, and :height Aaron Ecay
@ 2013-03-06  8:35               ` Nicolas Goaziou
  1 sibling, 0 replies; 17+ messages in thread
From: Nicolas Goaziou @ 2013-03-06  8:35 UTC (permalink / raw)
  To: emacs-orgmode

Hello,

aaronecay@gmail.com writes:

> This should now be handled.  The new patch will follow this email.  In
> related news, I have completed the FSF copyright assignment process, so
> the patch can be merged whenever it is deemed ready.

Your patch looks good. I have pushed it with minor stylistic changes.

Also, could you send the confirmation of your assignment process from
FSF to Bastien?

Thank you for your work.


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 17+ messages in thread

end of thread, other threads:[~2013-03-06  8:36 UTC | newest]

Thread overview: 17+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-02-24 18:51 [PATCH] ob-R.el, ox-latex.el: support for tikz graphics Aaron Ecay
2013-02-25 15:19 ` Nicolas Goaziou
2013-02-26 10:50   ` Achim Gratz
2013-02-26 12:25     ` Myles English
2013-02-26 13:21     ` Nicolas Goaziou
2013-02-26 14:33       ` Achim Gratz
2013-02-26 19:21       ` Aaron Ecay
2013-02-26 19:22         ` [PATCH] ox-latex: provide width and height options for images Aaron Ecay
2013-02-26 23:04           ` Rasmus
2013-02-27  2:02             ` Aaron Ecay
2013-02-27 18:40             ` Achim Gratz
2013-02-27  8:23           ` Nicolas Goaziou
2013-03-06  4:02             ` aaronecay
2013-03-06  4:04               ` [PATCH] ox-latex: Support tikz images, :width, and :height Aaron Ecay
2013-03-06  8:35               ` [PATCH] ox-latex: provide width and height options for images Nicolas Goaziou
2013-02-26 18:25 ` [PATCH] ob-R.el, ox-latex.el: support for tikz graphics Achim Gratz
2013-02-26 19:49   ` Aaron Ecay

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).