1

I'm porting my original Latex file to include Lualatex code, as I'm reading an input JSON file. I'm unable to get 'resizebox' to work with luacode, without it my table shows up but I want the table aligned to textwidth like in my original tex file. Any leads pelase? JOSN:

{
   "ID":"ProjectionParams",
   "tableEntries":{
      "PROJECTION":"UNIVERSAL TRANSVERSE MERCATOR (UTM)",
      "SEMI-MAJOR":"0",
      "INVERSE FLATTENING OF REFERENCE ELLIPSOID":"0",
      "SCALE FACTOR ALONG THE CENTRAL MERIDIAN":"0",
      "WORLD MAGNETIC MODEL":"0"
   }
}

MAIN.Tex

\documentclass[12pt]{article}

\usepackage[document]{ragged2e} \usepackage[utf8]{inputenc} \usepackage[T1]{fontenc} \usepackage[nohead,paperheight=11.0in,paperwidth=8.5in,left=0.5in,right=0.5in,top=0.5in,bottom=1.0in]{geometry}

%%%%% dynamic table package %%%%% \usepackage{datatool} \DTLsetseparator{,}% Set the separator between the columns. \usepackage{longtable} \usepackage{tabularx} \usepackage{amsmath}

\usepackage{hhline} \usepackage{multicol} \usepackage{multirow} \usepackage{booktabs} \usepackage{makecell} \usepackage{color, colortbl} \usepackage[table]{xcolor} \usepackage{graphicx}

\usepackage{tikz} \usepackage{float} \usetikzlibrary{shapes.symbols,shapes.geometric,shadows,arrows.meta} \tikzset{>={Latex[width=1.5mm,length=2mm]}}

%conditionals package \usepackage{xparse} \usepackage{etoolbox}

\renewcommand{\arraystretch}{1.5} %cell height \usepackage{luacode}

\begin{document} \begin{FlushLeft} ISOGONIC LINES PROGRAM \end{FlushLeft}

\input{projection.tex} \end{document}

projection.tex ( TEX):

\section*{PROJECTION PARAMETERS}
\begin{tikzpicture}
\begin{scope}[yscale=1,xscale=-1,xshift=-7.5in]
    \draw (-0.04in,-0.22in) -- (7.54in,-0.27in); 
\end{scope}

\end{tikzpicture}

%%% Tex input to table %%% \begin{table}[H] \resizebox{\textwidth}{!}{% \begin{tabular}{cc}

\hline %row no:1

\multicolumn{1}{|p{3.55in}}{\textbf{PROJECTION}}& \multicolumn{1}{|p{3.55in}|}{\PROJECTION}\ \hhline{--} %row no:2

\multicolumn{1}{|p{3.55in}}{\textbf{SEMI-MAJOR AXIS OF REFERENCE ELLIPSOID (METERS):}} & \multicolumn{1}{|p{3.55in}|}{\SEMIMAJOR} \ \hhline{--} \end{tabular} } \end{table}

projection.tex (Lualatex ):

\section*{PROJECTION PARAMETERS}
\begin{tikzpicture}
\begin{scope}[yscale=1,xscale=-1,xshift=-7.5in]
    \draw (-0.04in,-0.22in) -- (7.54in,-0.27in); 
\end{scope}
\end{tikzpicture}

\begin{luacode} local json = require("json") local file = io.open("data.json") tab = json.parse(file:read("*all")) file:close()

tex.print("\\begin{table}[H]")
tex.print("\\resizebox{\textwidth}{!}{%")
tex.print("\\begin{tabular}{|c|c|}")
tex.print("\\hline")

tex.print("{PROJECTION} &")
tex.print(tab["tableEntries"]["PROJECTION"])
tex.print("\\\\ \\hhline{--}")
tex.print("{SEMI-MAJOR AXIS OF REFERENCE ELLIPSOID (METERS)} &")
tex.print(tab["tableEntries"]["SEMI-MAJOR"])
tex.print("\\\\ \\hhline{--}")

tex.print("\\end{tabular}")
tex.print("\\}")
tex.print("\\end{table}")

\end{luacode}

End Table: protable

24suns
  • 37
  • 1
    Wouldn't it be easier to make sure both columns ar enarrow enough to fit onto the page in the first place, instead of trying to scale the table, which would in turn lead to inconsistent font sizes? – leandriis May 10 '21 at 20:01
  • Seomthing like \begin{tabular}{|*{2}{p{\dimexpr 0.5\textwidth-2\tabcolsep-1.5\arrayrulewidth}|}} \hline \textbf{PROJECTION}& \PROJECTION\\ \hhline{--} \textbf{SEMI-MAJOR AXIS OF REFERENCE ELLIPSOID (METERS):} & \SEMIMAJOR \\ \hhline{--} \end{tabular} should result in a table that fits into the available space. – leandriis May 10 '21 at 20:01
  • 2
    using \resizebox with tables should be avoided at all costs, it is bound to produce fonts and rule widths inconsistent with the rest of the document. – David Carlisle May 10 '21 at 20:18
  • @leandriis thank you...this is very first document and since the table output in tex looked fitted with the page...I chose this approach...I'm unable to get lua to print the table when I try to include your suggestion to alter the table...error: invalid escape sequence near '"{|*{2}{p{\d'. – 24suns May 10 '21 at 20:41
  • Which document class do you employ? Which packages do you load? How or where are \PROJECTION and \SEMIMAJOR defined? Where might the json module be obtained from? – Mico May 10 '21 at 20:44
  • @DavidCarlisle thank you David...what do u suggest that I try to align the table and text in lua? – 24suns May 10 '21 at 20:45
  • 1
    I'd get the table sorted out in tex first the lua code is just obscuring things, for instance why is the tablular using cc and then every cell is usiing \multicolumn to replace that by p ? If your table uses two p columns you know in advance how wide it is, so you can choose a width that fits so no scaling is needed. – David Carlisle May 10 '21 at 20:50
  • I did not ask what's in the file data.json. I asked how to obtain the json module. – Mico May 10 '21 at 20:50
  • 1
    You should fix your example so people can run it and debug the issue. add a small but working json file and make the tex a complete document with \documentclass to \end{document} – David Carlisle May 10 '21 at 20:51
  • @Mico using the json lua package from http://regex.info/blog/lua/json – 24suns May 10 '21 at 21:07
  • 1
    no one can run the code posted or see the problem that you are seeing. Your main.tex has no documentclass for example. the lua seems to be unrelated to your question, it is just writing tex code so you could ask a question just using tex that asks about the table alignment. \resizebox and tabular etc are not handled by the lua code at all, to Lua it is just a string that is written to the tex document to be interpreted as tex. – David Carlisle May 10 '21 at 21:14
  • apologies...I was too soon to save my edits and missed out a chunk of code...so the reason for having the lua code is I'm trying to build a dynamic table with the key value pairs in the json input file...so my first step was to see if I could print the tex code as is in lua that creates the table – 24suns May 10 '21 at 21:21
  • 1
    But as shown in Mico's answer your actual question (as far as I can tell) is completely unrelated to Lua. Lua does not interpret the tex commands at all it just treats it as a string so once you know what latex you want to write, the question "how do I write this tex code from lua" is the same whether you are writing tables or footnotes or tikz plots or anything else: just generate the tex code you want as a string then add it to the document with tex.print there is nothing special about tables in that step. – David Carlisle May 11 '21 at 07:38
  • You still haven't answered my earlier question, about how \PROJECTION and \SEMIMAJOR are defined. (Aside: What's with the ALL-UPPERCASE stuff?) And your posting continues to provide no reason for why one might want to employ a luacode environment. Sadly, my mind-reading skills are simply worthless. Unless you start providing actionable information, the odds that someone will manage to figure what you're trying to achieve are bleak. – Mico May 11 '21 at 08:59
  • @Mico-
    1. projection and semi major are defined in a .tex file ----

    \newcommand{\PROJECTION}{UNIVERSAL TRANSVERSE MERCATOR (UTM)} \newcommand{\SEMIMAJOR}{6378137.000} --- 2. and the uppercase is how the mocked up WORD report that itry to rewrite in Latex 3. the reason for the luacode is read json file entries( cudnt do with plain tex) and loop through the objects to create a table with the json entries

    – 24suns May 11 '21 at 10:23

1 Answers1

0

It looks like you wish to create a table whose overall width is equal to \textwidth and which has two equally wide columns, both of which are supposed to allow automatic line wrapping. If this interpretation is correct, the \resizebox, the c column type, and the \multicolumn instructions are all superfluous and/or extraneous. Just load the tabularx package and use its eponymous environment.

Oh, and PLEASE DON'T USE ALL-CAPS TERMS -- UNLESS YOU WANT TO COME ACROSS AS WANTING TO SHOUT AT YOUR READERS.

enter image description here

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{L}{>{\raggedright\arraybackslash}X}
\begin{document}
\section*{Projection parameters}

\begin{table}[ht!] \begin{tabularx}{\textwidth}{@{} LL @{}} \hline \textbf{Projection} & Projection \ \hline \textbf{Semi-major axis of reference ellipsoid (meters)} & Semimajor \ \hline \end{tabularx} \end{table}

\end{document}

Mico
  • 506,678